我正在使用收缩路线https://npmjs.org/package/shrinkroute在nodejs中建立链接。我收到错误500 ReferenceError:收缩器未定义
如何将rinkeroute传递给routes / index.js?有没有更好的方法通过传递查询字符串args创建url?
//app.js var app = express(); var shrinkr = shrinkroute( app, { "user": { path: "/user/:id?", get: routes.showOrListUsers } }); //url method works in app.js var url = shrinkr.url( "user", { id: 5, page:40, type:'a' } ); console.log(url); app.use( shrinkr.middleware ); //routes/index.js exports.showOrListUsers = function(req, res, next) { console.log(req.params); //shrinkr errors out in index.js var url2 = shrinkr.url( "users", {name: "foo"}); console.log(url2); }
一种解决方案是shrinkr使用app.set以下方法存储在您的应用程序对象中:
shrinkr
app.set
// app.js ... app.set('shrinkr', shrinkr); ...
在中routes/index.js,您可以通过req.app或res.app对象访问它:
routes/index.js
req.app
res.app
exports.showOrListUsers = function(req, res, next) { var shrinkr = req.app.get('shrinkr'); ... };