我想在 expressjs 中设置一些 catch all routes
我有三个级别
/module
/module/page
如果模块路由不存在发送到404路由 同样,如果模块存在但页面未发送到 404 路由。
html 格式的 404 回复
/api/controller/method
如上,但 404 等价物将在 json 中回复
正则表达式让我很难受,所以如果有人能帮我解决我的问题,我将不胜感激。 :) 我想过使用路由查找表,但我想尝试以“快速”方式进行。
最佳答案
只需设置路线 /模块 /module/page(或者你可能想要类似/module/:page_id 的东西)
并在所有要添加的路由的底部
app.use(function(req,res) {
res.render('404',
{ locals: {'title':'Not Found'}, },
function(err,str) { res.send(str,404); } );
如果没有任何匹配项,则捕获所有快照!
这是来自 google groups 中的那个线程 https://groups.google.com/forum/?fromgroups=#!topic/express-js/aYLM4e1I4XU
关于routes - 捕捉 expressjs 中的所有路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10942692/