const http = require('http'); const hostname = '127.0.0.1'; const port = 8080; const courses = [{name:'HTML'},{name:'CSS'},{name:'Javascript'},{name:'Node'}]; const server = http.createServer((req,res)=>{ const url = req.url; // what ? const method = req.method; // how, action ? if(url === '/courses'){ if(method === 'GET'){ res.writeHead(200, {'Content-Type': 'application/json'}); res.end(JSON.stringify(courses)); }else if (method === 'POST'){ const body = []; req.on('data', chunk=>{ console.log(chunk); body.push(chunk); }); req.on('end',()=>{ const bodyStr = Buffer.concat(body).toString(); console.log(bodyStr); //{"name":"test"} const course = JSON.parse(bodyStr); courses.push(course); console.log(course); res.writeHead(201); res.end(); }) } } }); server.listen(port,hostname,()=>{ console.log(`Server running at http://${hostname}:${port}`); });