Express/node.js路由器中的通配符

Ale*_*xis 8 node.js express

我想"摆脱"node.js中的路由器.目前,我所拥有的是这样的东西:

app.get '/thing1', (req, res) ->
    res.render 'thing1'

app.get '/thing2', (req, res) ->
    res.render 'thing2'
Run Code Online (Sandbox Code Playgroud)

有没有办法将这些崩溃到这样的事情:

app.get '/(*)', (req, res) ->
    res.render '(*)'
Run Code Online (Sandbox Code Playgroud)

PS:我正在使用coffeescript,但任何语言的答案都可以

Jon*_*Ong 28

app.get('/:thing', function (req, res) {
  res.render(req.params.thing)
})
Run Code Online (Sandbox Code Playgroud)

  • 请注意,使用":thing"将不匹配"/ path/with/slashes"之类的路径. (7认同)

Ell*_*ter 5

来自http://expressjs.com/api.html#app.VERB:

app.get(/^(.*)$/, function(req, res, next){
  res.send(req.params[0]);
});
Run Code Online (Sandbox Code Playgroud)

工作要点:https: //gist.github.com/elliotf/5826944