http

PHOTO EMBED

Sun Jul 23 2023 09:15:43 GMT+0000 (Coordinated Universal Time)

Saved by @nelson22

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();
        }
    })
})

content_copyCOPY