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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay