I have code in my Node app that defines how static resources should be served with added security by helmet middleware and HTTPS redirection. And I added compression for efficiency. And I use some cookies.
But what is the best way to order these functions? Or doesn't it matter at all? I cannot seem to find documentation on this topic.
(general variables)
(app.set code)
...
// Security
app.use(function (req, res, next) {
res.locals.cspNonce = nonce;
next();
});
app.use(helmet());
// CSP
app.use(
helmet.contentSecurityPolicy({
directives: {
//some directives
}
})
);
app.use(
helmet.expectCt({
//some options
})
);
// Compress requests
app.use(compression());
// HTTPS redirection
app.use(function (req, res, next) {
if(!req.secure) {
return res.redirect(301, ['https://', req.get('Host'), req.url].join(''));
}
next();
});
// Cookies
app.use(cookieParser('String'));
app.use(function (req, res, next) {
let cookie = req.cookies.cookieName;
if (cookie === undefined) {
let randomNumber=Math.random().toString();
//some more logic
res.cookie('session', randomNumber, cookieConfig);
};
next();
});
// Static resources
const options = {
//some options
}
};
app.use(express.static('public', options));
...
(routing)
(server start)