CNode

这样设置路由为什么会无效?

Ffirhome发布于12 年前最后回复12 年前2 回复4649 浏览0 收藏

Express 4.0

app.use('/', function(req, res) { res.render('index', { title: 'index' }); });

app.use('/users',function(req, res) { res.render('users',{ title:'user' }) });

第二个无效。。。

app.use('/',index); app.use('/users',users);

单独写在文件里 exports 出来是有效的。。

不能将一批路由写一起吗?

查看回复

回复 (2)

G
GuoZhang#1·12 年前

app.use 是添加中间件(middleware)的 http://expressjs.com/4x/api.html#app.use ==> 访问 /users 时, '/' 会匹配它,然后执行。

应该使用 app.all(path, [callback...], callback)app.VERB(path, [callback...], callback) , 其中VERB就是HTTP方法,比如get, post, head等。

参与回复
登录后即可参与回复。登录