pass params from app.js to route.js

时间秒杀一切 提交于 2021-01-27 19:52:30

问题


I have app.js with the following code and I've additional file which is called router.js which handle request's such post/read/etc

this is the app.js

var express = require('express');
    module.exports = function () {
            var app = express();
             ....
            require('./router/routes')(app, express);
            return app;
        };

The router.js is looks like following

*/

module.exports = function (app, express) {

    var appRouter = express.Router();
    app.use(appRouter);
    appRouter.route('*')
.post(function (req, res) {

            handler.dispatch(req, res);
        })
        .get(function (req, res) {
            handelr.dispatch(req, res)
        })

There is a nice way to avoid to pass two parameters (app,express) ?


回答1:


Yes, change the way you structure your files so you can instead simply require app and express. Now you no longer need to wrap every module in a function or pass any parameters.

app.js

var express = require('express');
var app = express();
module.exports = app;

app.boot = function () {
    // require in middleware, routes, etc, then start listening.
    require('./middleware');
    require('./routes');
    // middleware that comes after routes, such as error handling
    require('./middleware/after'); 
    app.listen();
}
if (require.main === module) {
    //move execution to next tick so we can require app.js in other modules safely
    process.nextTick(app.boot);
}

routes.js

var express = require('express');
var app = require('./app');
var appRouter = express.Router();
app.use(appRouter);
appRouter.route('*');
appRouter.get('/foo', function () {...});
appRouter.post('/foo', function () {...});
module.exports = appRouter; // for unit testing, or you can use this to attach it in app.js instead.

Additionally, by attaching the boot function to app, you can now include this app into another express app and attach it as a router if needed without having to change anything. All you would have to do is require it in, attach it, and then execute app.boot to attach the needed middleware/routes. (useful for unit testing)




回答2:


There is no "nice" way to get rid of the passing of app. express you could "avoid" by simply adding var express = require('express'); at the top of the script.

The other option is to instead export your router and mount it in the parent script. For example:

router.js:

var appRouter = require('express').Router();
appRouter.route('*')
// ...

module.exports = appRouter;

app.js:

var router = require('./router');
var app = require('express')();
app.use(router);



回答3:


You don't need to pass express because you can just require it in routes.js without any additional overhead (You already require it in app.js so all you're getting is a reference to the same object).

Furthermore, your router doesn't need to know about app. In fact, it makes more sense to return a router from your routes.js and use it from app.js. This is called "separation of concerns"; app.js handles creation and modification of app while routes.js handles creation and modification of the routes.

app.js

var express = require('express');
var routes = require('./router/routes');

module.exports = function () {
  var app = express();
  app.use(routes);
  return app;
};

router.js

var express = require('express');

module.exports = function () {
  var appRouter = express.Router();
  appRouter.route('*')
  return appRouter;
};


来源:https://stackoverflow.com/questions/31881187/pass-params-from-app-js-to-route-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!