How to pass variable from app.js to routes/index.js?

后端 未结 4 1734
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 13:18

I\'m using shrinkroute https://npmjs.org/package/shrinkroute to make links in nodejs. I get error 500 ReferenceError: shrinkr is not defined

How to pass shrinkroute

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 13:39

    One solution would be to store shrinkr in your app object using app.set:

    // app.js
    ...
    app.set('shrinkr', shrinkr);
    ...
    

    In routes/index.js, you can access it through the req.app or res.app objects:

    exports.showOrListUsers = function(req, res, next) {
      var shrinkr = req.app.get('shrinkr');
      ...
    };
    

提交回复
热议问题