DEV Community

Smart Home Dan
Smart Home Dan

Posted on

Node.JS Basics: Handling Cookies

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

Top comments (0)