I am learning node.js and the course says there is something wrong with the following snippet.
const fs = require('fs');
const http = require('http');
const server = http.createServer((req, res) => {
if (req.headers['x-secret'] != process.env.SECRET )
res.writeHead(403).end('Secret incorrect');
let body = [];
req.on('data', chunk => {
body.push(chunk);
});
req.on('end', () => {
body = JSON.parse(Buffer.concat(body).toString());
fs.writeFileSync(body.filename, body.file);
res.writeHead(200).end('OK');
});
});
server.listen(7654);
Possible things I've found include:
https should be used instead of http (secure server)
Res.writeHead.end is not valid syntax. Res.writeHead and res.end
should be written separatelyfs.writeFile() should be used, not the async version
There's no failsafe built-in (?)
However the course seems to be saying that there's a big mistake, which I can't really find.
Please help!
Thanks
res.writeHead(200).end('OK');