Send JSON With Status
NodeJS
Hard
3 views
Problem Description
Return 201 JSON response for /create and 404 otherwise.
Output Format
Start server code.
Constraints
Use res.writeHead.
Official Solution
const http = require('http');
const server = http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/create') {
res.writeHead(201, { 'Content-Type': 'application/json' });
return res.end(JSON.stringify({ ok: true, site: 'meetcode' }));
}
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
});
server.listen(3007, () => console.log('http://localhost:3007'));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!