Deviopath › Node.js

Parse Cookies

Easy · Headers

Browsers send all cookies in one header. In Node, req.headers.cookie looks like "theme=dark; session=abc123".

Write a function parseCookies(header) that returns an object of cookie names to values:

- pairs are separated by ;, with optional spaces around them

- split each pair at the first = (values may contain =)

- decode values with decodeURIComponent

- a missing or empty header gives {}

Next: Build a Query String