Reputation: 34507
I am creating a middleware in the nodejs application and I want to add header in the request in middleware and send it to the my endpoint index.js request.
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;
When I am trying to call it http://localhost:3000/ then it returns the 404 Not Found. Instead, it should redirect request to both middleware then to the index.js
GET / 404 8.700 ms - 139
Does anyone know whats wrong with it ?
Upvotes: 1
Views: 981
Reputation: 2943
Maybe I am missing something, but why you don't do:
var mw1 = require("./middlewares/middleware1");
var mw2 = require("./middlewares/middleware2");
app.use(mw1, mw2);
app.use("/", index);
And change your middleware to :
module.exports = function(req, res, next) {
next();
};
Upvotes: 3