我想"摆脱"node.js中的路由器。 目前,我拥有的东西看起来像这样:
app.get '/thing1', (req, res) ->
res.render 'thing1'
app.get '/thing2', (req, res) ->
res.render 'thing2'
有没有办法将它们折叠成这样的东西:
app.get '/(*)', (req, res) ->
res.render '(*)'
PS:我正在使用咖啡脚本,但是任何语言的答案都可以
app.get('/:thing', function (req, res) {
res.render(req.params.thing)
})
从 http://expressjs.com/api.html#app.VERB :
app.get(/^(.*)$/, function(req, res, next){
res.send(req.params[0]);
});
工作要点:https://gist.github.com/elliotf/5826944