简体   繁体   中英

How to pass a parameter to a self executing anonymous function

I have in my server.js this line of code:

 require('../routes/allRoutes')(app)

And this works fine when my allRoutes.js looks like this:

 module.exports = function(app){
    app.get("/", function(req, res){
      res.render ......
    });
 }

But what if my allRoutes.js looks like this:

  (function(allRoutes){

     app.get("/", function(req, res){
        res.render .....
      });
  })(module.exports)

How do I pass the app object in the anonymous, self executing function?

Nevermind, I figured it out:

1. server.js

 require('./routes/allRoutes').init(app); 

2. allRoutes.js

 (function(allRoutes) { allRoutes.init = function(app) { app.get("/", function (req, res) { res.send('Hello You'); }) }; })(module.exports); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM