简体   繁体   中英

Pass express() app variable from one file to another

Hello guys, first of all, I know here is a lot of similar posts, but unfortunately I still can't figure out my trouble.

I have a file server.js , where I declare my app variable and invoke some config stuff:

var app = express();
...

In my other file which is on path ./server/routes/PostApi.js I need to call code like this:

app.get('/api/posts', function(req, res) {
// use mongoose to get all posts in the database
Post.find(function(err, posts) {
    // if there is an error retrieving, send the error. nothing after res.send(err) will execute
    if (err) {
        res.send(err);
    }
    res.json(posts); // return all posts in JSON format
}); 
...

on the end of this file i call:

module.exports = PostApi;

which is required in server.js mentioned at first:

var PostApi = require('./server/routes/PostApi');

Please, what is best practice these days for passing app variable to my api file? Thank you!

So the direct answer to your question is that you can turn your route code into a function that accepts the app as a param:

module.exports = function(app){

   app.get('/api/posts', function(req, res) {
   // use mongoose to get all posts in the database
   Post.find(function(err, posts) {
       // if there is an error retrieving, send the error. nothing after       res.send(err) will execute
       if (err) {
           res.send(err);
       }
       res.json(posts); // return all posts in JSON format
   }); 
   ...
};

that said, it's also common for folks to instead build a router inside their routes and export that, for the app to then require and use:

var router = require('express').Router();

    router.get(''/api/posts', function(req, res) {
           // use mongoose to get all posts in the database
       Post.find(function(err, posts) {
           // if there is an error retrieving, send the error. nothing after       res.send(err) will execute
           if (err) {
               res.send(err);
           }
           res.json(posts); // return all posts in JSON format
       });

module.exports = router;

// in app.js

...
app.use(require('./myrouter.js'));

The Paul´s answer is correct, but without too many changes on your files you can do this.

module.exports = function(app){

   app.get('/api/posts', function(req, res) {
   ....
   }); 
};

in app.js

var PostApi = require('./server/routes/PostApi')(app);

This works for me :

index.js

var express = require('express'); // include
var moduleA = require('./moduleA'); // include

var app = express();
var moduleA = new moduleA();

moduleA.execute(app);

app.listen(process.env.PORT || 8080);

moduleA.js

function moduleA() {
}

moduleA.prototype.execute = function(app) {
    // publish endpoints
    app.get('/register',
        function(req, res)
        {
            res.type('text/plain');
            res.send('Hello world express!!');
        }
    );
};


module.exports = moduleA;

And test it with:

http://localhost:8080/register

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