28 lines
1.1 KiB
JavaScript
28 lines
1.1 KiB
JavaScript
import http from 'node:http';
|
|
|
|
const host = process.env.COOKIE_SERVER_HOST ?? '127.0.0.1';
|
|
const port = Number(process.env.COOKIE_SERVER_PORT ?? '18080');
|
|
const cookieName = process.env.COOKIE_NAME ?? 'phase06_cookie';
|
|
const cookieValue = process.env.COOKIE_VALUE ?? 'roundtrip-ok';
|
|
|
|
const server = http.createServer((req, res) => {
|
|
const url = new URL(req.url ?? '/', `http://${host}:${port}`);
|
|
if (url.pathname === '/set') {
|
|
res.setHeader('Set-Cookie', `${cookieName}=${cookieValue}; Path=/; SameSite=Lax`);
|
|
res.setHeader('Content-Type', 'application/json');
|
|
res.end(JSON.stringify({ ok: true, action: 'set', cookieName, cookieValue }));
|
|
return;
|
|
}
|
|
if (url.pathname === '/echo') {
|
|
res.setHeader('Content-Type', 'application/json');
|
|
res.end(JSON.stringify({ ok: true, cookie: req.headers.cookie ?? '' }));
|
|
return;
|
|
}
|
|
res.statusCode = 404;
|
|
res.setHeader('Content-Type', 'application/json');
|
|
res.end(JSON.stringify({ ok: false, error: 'not found' }));
|
|
});
|
|
|
|
server.listen(port, host, () => {
|
|
console.log(JSON.stringify({ ok: true, listening: `http://${host}:${port}` }));
|
|
});
|