gee*_*ter 1 routing routes parameter-passing node.js
我正在使用Nodejs.
Server.js
app.get('/dashboard/:id', routes.dashboard);
Run Code Online (Sandbox Code Playgroud)
路由/ index.js
exports.dashboard = function(req, res){
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将app.js中的'id'变量传递给仪表板功能.我该怎么做呢?
假设ExpressJS,您不需要传递它.
对于每个参数占位符(如:id),req.params应该具有保存值的匹配属性:
exports.dashboard = function (req, res) {
console.log(req.params.id);
};
Run Code Online (Sandbox Code Playgroud)
但是,这假设请求的URL通过动词和模式匹配路由.