[英]Error using express routes method in NodeJS
I'm trying to structure my NodeJS API in a different way, but I'm getting the following error when trying to import my routes:我正在尝试以不同的方式构建我的 NodeJS API,但是在尝试导入我的路线时出现以下错误:
/Users/pato/Documents/nodejs-bp-api/node_modules/express/lib/router/index.js:139 debug('dispatching %s %s', req.method, req.url); /Users/pato/Documents/nodejs-bp-api/node_modules/express/lib/router/index.js:139 debug('dispatching %s %s', req.method, req.url); ^ ^
TypeError: Cannot read property 'method' of undefined TypeError:无法读取未定义的属性“方法”
express.js file express.js 文件
.
.
app.use(cors());
app.use(config.api.prefix, apiRoutes());
API routes/index.js API 路由/index.js
const todo = require('../api/routes/todo');
const express = require('express');
const apiRoutes = () => {
const app = express.Router();
todo(app);
return app();
};
module.exports = apiRoutes;
todo.js route todo.js 路由
const todo = router => {
router.get('/', function(req, res, next) {
res.json({ msg: 'This is CORS-enabled for all origins!' });
});
};
return app();
should just be return app;
应该只是return app;
(with no parentheses). (没有括号)。 A router is called as a function by express when handling a request, not during setup.路由器在处理请求时被称为 function,而不是在设置期间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.