我正在nodejs应用程序中创建一个中间件,我想在中间件的请求中添加头文件并将其发送到我的端点index.js请求。
middleware1.js
exports.mw1 = function(req, res, next) {
next();
};
middleware2.js
exports.mw2 = function(req, res, next) {
next();
};
app.js
var mw1 = require("./middlewares/middleware1");
var mw2 = require("./middlewares/middleware2");
var combinedMiddleware = function compose(middleware) {
return function(req, res, next) {
connect
.apply(null, middleware.concat(next.bind(null, null)))
.call(null, req, res);
};
};
app.use(combinedMiddleware([auth, audit]));
app.use("/", index);
index.js
var express = require("express");
var router = express.Router();
router.get("/", function(req, res) {
res.send("Welcome!!");
});
module.exports = router;
当我尝试将其调用http://localhost:3000/时,它会返回404 Not Found。相反,它应该将请求重定向到中间件,然后重定向到index.js
GET / 404 8.700 ms - 139
有谁知道它有什么问题吗?
答案 0 :(得分:2)
也许我错过了什么,但为什么你不这样做:
var mw1 = require("./middlewares/middleware1");
var mw2 = require("./middlewares/middleware2");
app.use(mw1, mw2);
app.use("/", index);
将您的中间件更改为:
module.exports = function(req, res, next) {
next();
};