Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

nodejs: http:send HTML to the Browser

const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
    console.log(req.url, req.method);

    // Set Header Content
    res.setHeader('Content-Type', 'text/html');

    fs.readFile('./views/index.html', (err, data) => {
        if (err) {
            console.log('Oops!!! Have error' + err);
            res.end();
        } else {
            // res.write(data);
            res.end(data);
        }

    });
});

server.listen(3000, 'localhost', () => {
    console.log('Listening for request on PORT 3000');
});
 
PREVIOUS NEXT
Tagged: #HTML #Browser
ADD COMMENT
Topic
Name
3+5 =