node.js Express:单独的路由和控制器文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34174115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:37:29  来源:igfitidea点击:

Express: Separate route and controller files

node.jsexpress

提问by mindparse

I'm trying to split out some routes and their handler logic in Express in separate files. I have seen examples directory structures like on mean JS where separate route and controller files are used, so it's that approach I am trying to implement but am hitting an issue.

我试图在单独的文件中拆分 Express 中的一些路由及其处理程序逻辑。我已经看到了示例目录结构,例如在使用单独的路由和控制器文件的 mean JS 上,所以我正在尝试实施这种方法,但遇到了问题。

My server and routes are configured like so:

我的服务器和路由配置如下:

server.js

服务器.js

var express = require('express'),
  app = express(),
  bodyParser = require('body-parser'),
  routes = require('./routes/index')

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

/routes/index.js

/routes/index.js

module.exports = function(app) {
  var catalogues = require('../routes/catalogues');
  app.use('/catalogues-api', catalogues);
};

/routes/catalogues.js

/routes/catalogues.js

var catalogues = require('../controllers/catalogues');
module.exports = function(app) {

  app.route('/catalogues')
    .get(catalogues.apiGET)
    .post(catalogues.apiPOST);
};

/controllers/catalogues.js

/controllers/catalogues.js

var request = require('request');

exports.apiGET = function(req, res) {
  var options = prepareCataloguesAPIHeaders(req);
  request(options, function(err, response, body){
    res.send(body);
  });
};

exports.apiPOST = function(req, res) {
  var options = prepareCataloguesAPIHeaders(req);
  options.json = true;
  options.body = stripBody(req.body);
  request(options, function(err, response, body){
    res.send(body);
  });
};

When running the application and a GET request is made against /catalogues-api/catalogues I get an error thrown from node:

当运行应用程序并针对 /catalogues-api/catalogues 发出 GET 请求时,我收到从节点抛出的错误:

TypeError: undefined is not a function at module.exports (C:\Users\rparker\Documents\GitHub\testproj\src\server\routes\catalogues.js:4:7)

TypeError: undefined is not a function at module.exports (C:\Users\rparker\Documents\GitHub\testproj\src\server\routes\catalogues.js:4:7)

This is referencing the app.route declaration in my /routes/catalogues.js file. I have obviously missed something in my setup but I cannot figure it out.

这是引用我的 /routes/catalogues.js 文件中的 app.route 声明。我显然在我的设置中遗漏了一些东西,但我无法弄清楚。

Can someone please assist? Thanks

有人可以帮忙吗?谢谢

回答by ma08

in /routes/catalogues.js

/routes/catalogues.js

var express = require('express');
var router = express.Router();
var catalogues = require('../controllers/catalogues');

router.route('/catalogues')
.get(catalogues.apiGET)
.post(catalogues.apiPOST);
module.exports = router;

Check out the last part of this documentation

查看本文档的最后一部分

回答by Slim

You may find a really nice autoloader in npmcalled 'require.all' that may help you a lot in automating the process of linking routes, controllers and models. At the end of the documentation is given similar example that assumes that all you route files export functions that expect controllers as parameter and return an express.Router object.

你可能会在npm 中找到一个非常好的自动加载器,叫做“ require.all”,它可以帮助你自动化链接路由、控制器和模型的过程。在文档的末尾给出了类似的例子,假设你所有的路由文件导出函数,这些函数期望控制器作为参数并返回一个 express.Router 对象。