Express / node.js路由器中的通配符 [英] Wildcard in Express/node.js router

查看:177
本文介绍了Express / node.js路由器中的通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想摆脱node.js中的路由器。目前,我所看到的是这样的东西:

I would like to "get rid" of the router in node.js. Currently, what I have is something that looks like this:

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

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

有没有办法将它们折叠成这样的东西:

Is there a way to collapse these to something like this:

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

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

PS: I'm using coffeescript, but an answer in any language is OK

推荐答案

app.get('/:thing', function (req, res) {
  res.render(req.params.thing)
})

这篇关于Express / node.js路由器中的通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆