For every request that happens, I'd like to check if a parameter in the query string is set. If not, the application should send a certain message; otherwise, route as appropriate.
In app.js
:
app.use(function(req,res,next){
if(req.query.key === undefined) {
res.send("Sorry!");
}
req.db = db;
next();
});
app.use('/', routes);
When '/'
is requested without the param, Sorry!
is displayed. However, my ExpressJS app crashes with this error:
Error: Can't set headers after they are sent.
I'm not entirely sure why this is happening. I've tried moving the check to the route itself in index.js
, but I still get the same error.