如何将参数传递给路由? [英] How to pass parameter to routes?
本文介绍了如何将参数传递给路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Nodejs.
I am using Nodejs .
Server.js
app.get('/dashboard/:id', routes.dashboard);
路由/index.js
Routes / index.js
exports.dashboard = function(req, res){
}
我希望能够将 'id' 变量从 app.js 传递到仪表板函数.我该怎么做?
I want to be able to pass the 'id' variable from app.js to the dashboard function . How do I go about doing this ?
推荐答案
假设 ExpressJS,你应该不需要通过它.
Assuming ExpressJS, you shouldn't need to pass it.
对于每个参数占位符(如 :id
),req.params
应该有一个匹配的属性来保存值:
For each parameter placeholder (like :id
), req.params
should have a matching property holding the value:
exports.dashboard = function (req, res) {
console.log(req.params.id);
};
不过,这假设请求的 URL 与动词和模式匹配的路由.
Though, this assumes the requested URL matches the route by verb and pattern.
这篇关于如何将参数传递给路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文