DEV Community

loading...

Node.JS Basics: Handling Cookies

Smart Home Dan
Software Developer working on Enterprise Back End Systems.
・1 min read

Below is the test code I have for checking if my method for sending cookies is working.

Example Cookie Parsing Server

const express = require("express");
const app = express();
const cookieParser = require("cookie-parser");

app.use(cookieParser());

app.get("/test-cookie", (req, res) => {
    console.log(req.cookies);
    // Here we should have access to the Cookies sent.
    res.status(204);
});

app.post("/login", (req, res) => {
    res.writeHead(200, 
        {
            "Set-Cookie": "token=encryptedstring; HttpOnly",
            "Access-Control-Allow-Credentials": "true"
        }
    ).send();
});

app.listen(8080, () => {
    console.log("listening on port 8080 for Cookies...");
});
Enter fullscreen mode Exit fullscreen mode

Discussion (0)