DEV Community

Smart Home Dan
Smart Home Dan

Posted on

1 1

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)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay