With Express I have the following setup:
const express = requires("express");
const path = requires("path");
const bodyParser = requires("body-parser");
const cookieParser = requires("cookie-parser");
let server = express();
server.set("port", (process.env.PORT || 5000));
server.set("views", path.join(__dirname, "/views"));
server.set("view engine", "ejs");
server.use(cookieParser());
server.use(express.static(self.workingDirectory + "/public"));
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
server.use((req, res, next) => {
if ( req.cookies && typeof req.cookies["testcookie"] === "undefined" ) {
console.log("Setting cookie! Testcookie was not found");
res.cookie("testcookie", "test", {
maxAge : ((((1000*60)*60)*24)*7),
httpOnly: true
});
}
next();
});
server.get("/", (req, res) => { res.render("pages/index"); });
server.listen(server.get("port"), () => { console.log("Server started!"); });
Therefore, when I am in /, the cookie is set accordingly, but with each visit after that it continues to be set. Later, when I launched console.log(req.headers)
through the new middleware, it displays the following on every visit /, although a cookie must be set:

There is a cookie in the response headers:

Am I doing something wrong here? I can’t understand what’s wrong ... Is it cookie-parsernot intended to fill the req.cookiescookie from the property req.headers.cookie? Why req.headers.cookiecomes back empty. req.headers.cookiealso returns empty if I comment on anything related to cookie-parser.