How to handle POST request on my http server in js? Allready answered
I'm not sure how to explain it, but when I send a POST request to the server nothing happens. I'm missing the code on the server to handle the request. I am only interested in http module (because I want to understand it before I move on to Express) What to do from here? My request: fetch('http://127.0.0.1:5674', { method: 'POST', body: ' Anton', headers: {"content-type": "text/plain"} }) .then(response => console.log(response)) .catch(err => { console.log('failed', err) }) My server let http = require('http'); const server = http.createServer((req, res) => { if (req.method === 'POST'){ res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end(req.body); } if (req.url === '/') { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('hello'); } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('404 Not Found'); } }); server.listen(5674, '127.0.0.1', () => { console.log('Server is running') })