Preview:
const http = require('http');
// create server
const server = http.createServer((req, res) => {
    console.log("server started");
})
server.listen(3000, 'localhost', () => {
    console.log("listening to request at port 3000");
})

-----------------------------------------------------------

// sending plain text response to browser on server start
const server = http.createServer((req, res) => {
    res.setHeader('Content-Type', 'text/plain');
    res.write('Data is loaded.');
    res.end();
})

-----------------------------------------------------------

// sending html tag response to browser on server start
const server = http.createServer((req, res) => {
    res.setHeader('Content-Type', 'text/html');
    res.write('<h1>This is page heading</h1>');
    res.write('<p>This is paragraph line for this page.</p>')
    res.end();
})

-----------------------------------------------------------

// sending a html page response to browser on server start
const server = http.createServer((req, res) => {
    res.setHeader('Content-Type', 'text/html');
    fs.readFile('./views/index.html', (err, data) => {
        if(err){
            console.log(err);
            res.end();
        }else{
            res.write(data);
            res.end();
        }
    })
})

downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter