How to render normal html file in express.js

ExpressJs is a very popular framework for node.js, Express default provide stylus and jade tempting engine but if you don’t have much knowledge about stylus and jade then don’t worry you can use your old method normal html and css.


You can render easily your normal html page in express by res.sendFile() function in express routing.

app.js

var express = require("express");
var app     = express();
var path    = require("path");
app.get('/',function(req,res){
  res.sendFile(path.join(__dirname+'/index.html'));
  //__dirname : It will add the path of your project folder
});
 
app.listen(5000);
console.log("Application running on port 5000");

Where index.html is your main landing page if you hit the url http://localhost:5000 then it’ll be your main landing page.

And if you have more pages then you can use same method to render your all html pages.
copy below code and paste it in your main app.js file

// rendering about us page
app.get('/about',function(req,res){
  res.sendFile(path.join(__dirname+'/about.html'));
});
// rendering contact us page.
app.get('/contact',function(req,res){
  res.sendFile(path.join(__dirname+'/contact.html'));
});

If you like this post please don’t forget to subscribe My Public Notebook for more useful stuff.