Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
parsing cookies from http.createServer()

parsing cookies from http.createServer()

To parse cookies, we can use the cookie module's cookie.parse() function:

var cookie = require('cookie');
var http = require('http');

var server = http.createServer(function (req, res) {
    var cookies = cookie.parse(req.headers.cookie);
    res.end('I got these cookies: ' + JSON.stringify(cookies) + '\n');
});
server.listen(8000);

First start the server:

node server.js

then in another terminal, we can use curl to send a cookie header:

$ curl -H 'cookie: beep=boop; xyz=123' http://localhost:8000
I got these cookies: {"beep":"boop","xyz":"123"}

see also

[sending cookies from http.createServer()]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment