,How to render html in node jsnode js render html with data, node js render html with css, node js render html without express, how to link node.js to html, rendering in node js, pass value from node js to html, node js html, node js html example, getting data from node.js file and displaying it in html/js page, node js render html to image, node js render html template, node js render html with params, node js render html string, node js render html example, node js render html file, node js render html with js, node js render html data, nodejs express render html file, render html file using node js, node js how to render html, node js render index.html, render dynamic html in node js, node js render html page, render html page using node.js, node js render html response, node.js render static html, node.js express render static html, node js render html tag

How to render html in node js

Node JS allows you to create a custom web server according to your need. In this tutorial, we’re going to install node js and we are also writing our very first application let’s get started. Go to nodejs.org to download node js for your machine to install it.

download node js

Once the installation is finished you can check that everything was successful by running node – V in the terminal or command line. it is available now that we have node we can jump right into writing our first application because the cool thing about node js is we don’t have to install any separate server or something like.

Create node js server

Create a new file and I will call it server.js because we’re writing our first server so, we want to see
something in the browser right from the beginning. Node js provide certain functionalities which allow us to create a server like functionality or not the only server like but a server functionality, for example, it should listen to a certain port on a certain domain and all requests coming in it should be handled in a certain way and this is exactly what we are going specify in this server file. See code example below:-


var http = require('http');

function onRequest(request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('Hello VR Softcoder');
    response.end();
}

http.createServer(onRequest).listen(8000);

Render Html File

Create an HTML file in your node js application directory. To render HTML files we are going to use FS (File System in our application). With this file system module, we can read the file if exits in our application directory and send it to the client with response.

The read file method takes three arguments, the first one is the powerful body once we’ve in now this is going to be our index.html file, leave the second argument to null and third will be callback function. See code example below:-


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

function onRequest(request, response) {
    response.writeHead(200, {'Content-Type': 'text/html'});
    fs.readFile('./index.html', null, function(error, data) {
        if (error) {
            response.writeHead(404);
            response.write('File not found!');
        } else {
            response.write(data);
        }
        response.end();
    });
}

http.createServer(onRequest).listen(5000);

Render Html File

Create an HTML file in your node js application directory. To render HTML files we are going to use FS (File System in our application). With this file system module, we can read the file if exits in our application directory and send it to the client with response.

The read file method takes three arguments, the first one is the powerful body once we’ve in now this is going to be our index.html file, leave the second argument to null and third will be callback function. See code example below:-


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

function onRequest(request, response) {
    response.writeHead(200, {'Content-Type': 'text/html'});
    fs.readFile('./index.html', null, function(error, data) {
        if (error) {
            response.writeHead(404);
            response.write('File not found!');
        } else {
            response.write(data);
        }
        response.end();
    });
}

http.createServer(onRequest).listen(5000);

Create Routes For Node js

We are still a little bit away from the real website. So for Node js routing create a separate javascript file where we will handle the requests. I will create a new javascript file named app.js. I will export a function which should be useable in server.js. I will create two routes The index route (which will just be a slash at the end of our URL) the login route when the user types slash login.

We need to include the url module in app.js to make routes and FS to read the file. See the code example below:-


var url = require('url');
var fs = require('fs');

function renderHTML(path, response) {
    fs.readFile(path, null, function(error, data) {
        if (error) {
            response.writeHead(404);
            response.write('File does not exists!');
        } else {
            response.write(data);
        }
        response.end();
    });
}

module.exports = {
  handleRequest: function(request, response) {
      response.writeHead(200, {'Content-Type': 'text/html'});

      var path = url.parse(request.url).pathname;
      switch (path) {
          case '/':
              renderHTML('./index.html', response);
              break;
          case '/login':
              renderHTML('./login.html', response);
              break;
          default:
              response.writeHead(404);
              response.write('Route not defined');
              response.end();
      }

  }
};

Now include the app.js file to server.js and run below command to see output:-


node app.js

On the main URL, index.html will be rendered and when we hit /login URL it will render the login.html file.

Post Created 38

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top