NodeJS Program to Set Cookie Header with Explanation
NodeJS
Medium
HTTP & APIs
37 views
1 min read
81 words
This problem helps you practice core NodeJS fundamentals in a practical way. It builds intuition around cookie, set, header. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Return a response that sets a cookie and prints a message.
Input Format
No input.
Output Format
Start server code.
Constraints
Set HttpOnly cookie for demo.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader('Set-Cookie', 'session=meetcode123; HttpOnly; Path=/');
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Cookie set');
});
server.listen(3004, () => console.log('http://localhost:3004'));
Output Example
No sample I/O is provided for this question.
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Return a response that sets a cookie and prints a message.
Input / Output
Output
Start server code.
Constraints
Set HttpOnly cookie for demo.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader('Set-Cookie', 'session=meetcode123; HttpOnly; Path=/');
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Cookie set');
});
server.listen(3004, () => console.log('http://localhost:3004'));
Solutions (0)
No solutions submitted yet. Be the first!