Req.headers.cookie is empty, even with cookies in request headers

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 exists and testcookie is undefined within req.cookies
    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), /* expire a week from today */
            httpOnly: true /* document.cookie doesn't return this cookie */
        });
    }
    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.

+4
2

, - . ( ). npm:

"dependencies": {
    "body-parser": "^1.17.2",
    "cookie-parser": "^1.4.3",
    "express": "^4.15.3"
  } 

(exampleMiddleWare), cookie .

const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");

let server = express();

server.set("port", (process.env.PORT || 5000));

server.use(cookieParser());

server.use(bodyParser.json());
server.use(bodyParser.urlencoded({extended: true}));

server.use((req, res, next) => {
    // if req.cookies exists and testcookie is undefined within req.cookies
    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), /* expire a week from today */
            httpOnly: true /* document.cookie doesn't return this cookie */
        });
    }
    next();
});

const exampleMiddleWare = (req, res, next) => {
    res.hasTestCookie = !!req.cookies.testcookie
    next();
};

server.get("/", exampleMiddleWare, (req, res) => {
    res.send(`<h1>Cookie Test</h1><h2>Cookie Found: ${res.hasTestCookie}</h2>`);
});

server.listen(server.get("port"), () => {
    console.log("Server started!");
});

, , , , cookie .

+1

, req.headers.cookie . cookie req.header.

    const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const cookieParser = require("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 exists and testcookie is undefined within req.cookies
    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), /* expire a week from today */
            httpOnly: true /* document.cookie doesn't return this cookie */
        });
    }
    next();
});
server.get("/", (req, res) => { console.log(req.headers);
  res.send(`<h1> hello world </h1>`); });
server.listen(5000, () => { console.log("Server started!"); });
0

Source: https://habr.com/ru/post/1679139/


All Articles