In this section, we will be creating a node script that will allow us to send a JSON feedback to the user upon a successful request. Let's take a look at the app.js file that accomplishes this task:
const http = require('http');
const port = 3300;
http.createServer((req, res) => {
res.writeHead(200, {
"Content-Type": "application/json"
});
res.write(JSON.stringify({
greet : "Hello Readers!"
}));
res.end();
}).listen(port);
console.log(`Node Server is running on port : ${port}`)
The changes required to send JSON data are highlighted in the preceding snippet. The above script consists of a JSON object with greet as the key and Hello Readers! as the value. The object is first stringified as the responses provided are always in string or buffer form. Moreover, we need to provide the content-type...