Create and Run a html page in nodejs in Windows OS

Create and Run a html page in nodejs in Windows OS

n this blog, I’m explaining how to create an html page in nodejs and run in browser.

1.   Let's start with the server module. Create the file server.js in the root directory of your project, and fill it with the following code:

var http = require('http'),
    fs = require('fs');
    fs.readFile('./index.html', function (err, html) {
    if (err) {
        throw err;
    }
    http.createServer(function (request, response) {
        response.writeHeader(200, { 'Content-Type': 'text/html' });
        response.write(html);
        response.end();
    }).listen(8888);
});

2.  Create a html page “index.html” write code



<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <title></title>
</head>
<body>
    <center><h3>Hello World</h3></center>
</body>
</html>

1.       Open command prompt and go to your file location.

2.       Execute your server.js file like this

Now start your server…

1.    Now, open your browser and point it at http://localhost:8888/. This should display a web page that says 'Hello World'.

   Output 

 


in my next post i'll explain about Angular JS

Last updated:2/3/2020 3:19:10 AM
Anonymous User

Anonymous User

I am a content writter !

0 Comments

Leave Comment