简体   繁体   English

如何将应用程序实例传递给 ExpressJs 中的路由文件?

[英]How can pass the app instance to route file in ExpressJs?

I'm using Express framework to develop my backend APIs, but I encounter with one issue today.我正在使用 Express 框架来开发我的后端 API,但我今天遇到了一个问题。 I have one server.js (entry) file from there I want to pass app instance to routes/api.js file.我从那里有一个server.js (条目)文件,我想将app实例传递给routes/api.js文件。 where I wanted to check if endpoint prefix is /api/v2 then I want to call the routes/index.js file.我想检查端点前缀是否为/api/v2然后我想调用routes/index.js文件。 But I'm unable to achieve this approach.但我无法实现这种方法。 I'm adding my code of server.js routes/api.js & routes/index.js files below.我在下面添加了server.js routes/api.jsroutes/index.js文件的代码。

Server.js服务器.js

/** Core Packages */

/** NPM Packages */
const express = require('express');

/** Custom Packages */
const routerApi = require('./routes/api');
const dbConnect = require('./config/database');

const app = express();
dbConnect();

app.use(express.json());
app.use(express.urlencoded({extended: true}));

routerApi(app);

const PORT = 4000;

/** Creating a Server */
const server = app.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});

routes/api.js路线/api.js

/** Core Packages */

/** NPM Packages */

/** Custom Packages */

const apiRouter = (app) => {
  app.use('/api/v2', require('./index')(app));
};

module.exports = apiRouter;

routes/index.js路线/index.js

/** Core Packages */

/** NPM Packages */

/** Custom Packages */

const routes = (app) => {
  app.all('/*', (req, res, next) => {
    if (req.path === '/')
      return res.status(200).send({msg: 'Welcome to landing page.'});
  }
}

module.exports = routes;

Error错误

TypeError: Router.use() requires a middleware function but got a undefined
    at Function.use (/home/admini/Documents/skreem-api-node/node_modules/express/lib/router/index.js:458:13)
    at Function.<anonymous> (/home/admini/Documents/skreem-api-node/node_modules/express/lib/application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (/home/admini/Documents/skreem-api-node/node_modules/express/lib/application.js:217:7)
    at apiRouter (/home/admini/Documents/skreem-api-node/routes/api.js:9:7)
    at Object.<anonymous> (/home/admini/Documents/skreem-api-node/server.js:52:1)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
    at internal/main/run_main_module.js:21:11

Can anyone please help me out to resolve this problem?谁能帮我解决这个问题? Any kind of help will be appriciated.任何形式的帮助都将得到应用。 Thanks You!感谢您!

In server.js file, add the following at the bottom of your file:server.js文件中,在文件底部添加以下内容:

module.exports = app;

And then require server.js in the files where it is needed:然后在需要的文件中 require server.js

const app = require('path to server.js file');

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

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