Set Cookie Header

Set Cookie Header

Medium NodeJS HTTP & APIs 38 views
Explanation Complexity

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.

Input / Output Format

Input Format
No input.
Output Format
Start server code.
Constraints
Set HttpOnly cookie for demo.

Example Solution (Public)

NodeJS
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'));

Official Solution Code

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'));
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.