Reputation: 26415
In the next example, the application is using a m
middleware.
const express = require("express");
const app = express();
const m = () => (req, res, next) => {
next();
};
app.use(m());
app.get("/", (req, res) => {
res.sendStatus(200);
});
app.listen(5000);
if I (by accident) passed the function itself instead of the result of calling it:
-app.use(m());
+app.use(m); // causes express server hangs
the express server will hangs.
How can I debug this issue?
Upvotes: 2
Views: 1385
Reputation: 707436
m
is a function that returns a middleware function when you call it.
To get the middleware function you have to call it with m()
. If you just pass m
, then that's not a middleware function and executing it just immediately returns another function and never calls next()
so when express executes m
, it runs, immediately returns another function (which is ignored by Express) and your request is never handled.
Also, there's NO reason for making a function that returns a function in the case you show either. Just make a function and pass a reference to that function as middleware.
function m(req, res, next) {
next();
}
app.use(m);
Or inline:
app.use(function(req, res, next) {
next();
});
Upvotes: 1