嗨,我正在尝试将一些依赖项注入到expressjs路由中间件中。
通常,您会在主应用中执行以下操作:
const express = require('express');
const userRouter = require('./routes/users.js');
const app = express();
app.use('/users', userRouter);
在您的users.js
中,您将看到类似的内容:
const express = require('express');
const router = express.Router()
router.post('/user', function (req, res, next) {...}
router.get('/user/:id', function (req, res, next) {...}
router.put('/user/:id', function (req, res, next) {...}
router.delete('/user/:id', function (req, res, next) {...}
但是我想传递一些依赖关系,例如服务URL,并且文档中不清楚如何执行此操作,我认为是这样的:
const express = require('express');
function userRoutes(options) {
const router = express.Router();
router.post('/user', function (req, res, next) {...}
router.get('/user/:id', function (req, res, next) {...}
router.put('/user/:id', function (req, res, next) {...}
router.delete('/user/:id', function (req, res, next) {...}
return router
}
module.exports.userRoutes = userRoutes;
然后在我的主应用程序中将其使用为:
const userRouter = require('./routes/users.js');
const app = express();
app.use('/users', userRouter.userRoutes(options));
但是我得到了
Users/jm/Private/Projects/api-gateway/node_modules/express/lib/router/index.js:458
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a undefined
at Function.use (/Users/jm/Private/Projects/api-gateway/node_modules/express/lib/router/index.js:458:13)
at EventEmitter.<anonymous> (/Users/jm/Private/Projects/api-gateway/node_modules/express/lib/application.js:220:21)
at Array.forEach (native)
at EventEmitter.use (/Users/jm/Private/Projects/api-gateway/node_modules/express/lib/application.js:217:7)
at Object.<anonymous> (/Users/jm/Private/Projects/api-gateway/app.js:28:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
对任何建议/解决方案表示赞赏。
答案 0 :(得分:0)
我已经尝试过您的代码,并且对我有用。这些是我的文件,以防万一。
package.json:
{
"name": "problem",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4"
}
}
index.js
const express = require('express');
const http = require('http');
const userRouter = require('./routes/users.js');
const app = express();
const options = {};
app.use('/users', userRouter.userRoutes(options));
const server = http.createServer(app);
server.listen(3000);
console.log('listening on port 3000');
routes / users.js
const express = require('express');
function userRoutes(options) {
const router = express.Router();
router.post('/user', function (req, res, next) {res.json({done:true})})
router.get('/user/:id', function (req, res, next) {res.json({done:true})})
router.put('/user/:id', function (req, res, next) {res.json({done:true})})
router.delete('/user/:id', function (req, res, next) {res.json({done:true})})
return router
}
module.exports.userRoutes = userRoutes;
当我运行npm start
时,它开始监听端口3000。然后它开始。