简体   繁体   English

将express()应用变量从一个文件传递到另一个文件

[英]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: 我有一个文件server.js ,在其中声明我的应用程序变量并调用一些配置内容:

var app = express();
...

In my other file which is on path ./server/routes/PostApi.js I need to call code like this: 在我的其他文件中,该文件位于路径./server/routes/PostApi.js中,我需要这样调用代码:

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: 首先提到的server.js中需要:

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

Please, what is best practice these days for passing app variable to my api file? 拜托,如今将应用变量传递到我的api文件的最佳做法是什么? 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.js中

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

The Paul´s answer is correct, but without too many changes on your files you can do this. Paul的答案是正确的,但是无需对文件进行太多更改就可以做到这一点。

module.exports = function(app){

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

in app.js 在app.js中

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

This works for me : 这对我有用:

index.js 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 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 http:// localhost:8080 / register

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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