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

后端 未结 4 1728
没有蜡笔的小新
没有蜡笔的小新 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:31

    A bit late to the party, but the following works as well:

    app.js

    var my_var = 'your variable';
    
    var route = require('./routes/index')(my_var);
    app.get('/', route);
    

    and meanwhile in route.js

    var express = require('express')
    ,   router = express.Router()
    
    // Router functions here, as normal; each of these
    // run only on requests to the server
    
    router.get('/', function (req, res, next) {
        res.status(200).end('Howdy');
    });
    
    
    module.exports = function(my_var){
    
        // do as you wish
        // this runs in background, not on each
        // request
    
        return router;
    }
    

提交回复
热议问题