Node provides very low level api; thus it is very difficult to host your static files using only node. There are several Node modules which do this work, like node-static, paperboy and http-server. Today we are gonna show you how to to setup a complete Node server using the most popular module of Node, that is express. We are preferring express for two reasons. First, this is a rich module, which helps us to develop a complete application. Second, this is very simple to use.

I’m assuming that you already know how to scaffold a Node app; if not, go through our beginner’s guide to Node.js.

App scaffolding and folder structures

Make your project folder and run the following two commands which will scaffold the app and install the express for you.

npm init
npm install express

After executing the above commands your folder structure will be like the following.
installed express

Now we will create two more folders. One named “_public”, which is going to have all our client side files, i.e. html, css, client side scripts etc. Another folder is “_application”, which will contain the core Node application, that handle routes, database connectivity, business logic etc.

There will also be a server.js file (I prefer to name it as “server.js”, as it will handle the server configurations and also starts the server. But you can name it by your choice). Well after following all these steps, your folder will look like:
node server folder structure

Code server.js to serve static files

By default Node searches for corresponding routes when a request comes. A http request for a file also considered as a routes and Node searched for the corresponding handling. To serve the static files you need to configure it with express. Here goes the code:

var express = require('express');
var app = express();
var port = 8080;
app.use(express.static(__dirname + '/_public')); // set the static files location
console.log("App listening on port " + port);

Now your projectFolder/_public location has become the index location. You can put your index.html in _public folder and http://localhost:8080/ will display that in your browser. You can put you client side allication inside _public folder.

Create api routes

Now it’s turn to put your business node code in the server. For that the best practice will be to put them all in the _application folder. For example, we are going to put a file route.js there, which will handle my api routes.
Here comes my route.js

module.exports = function(app) {

// get some data
app.get('/api/someData', function(req, res) {
  //do stuff to get data
  var data={somedata:[1,2,3,4]}

// save a data'/api/saveData', function(req, res) {
  //do stuffs to save

// delete a data with id
app.delete('/api/delete/:data_id', function(req, res) {
  var id=req.params.data_id;
  //do stuffs to delete data of id='id'


To use route.js properly, we need to do something in server.js too. Here is the new server.js

var express = require('express');
var app = express();
var port = 8080;
app.use(express.static(__dirname + '/_public')); // set the static files location
require('./_application/routes.js')(app); //to set the routes for the app.
console.log("App listening on port " + port);

The line#5 helps to export the route functions.
So now, your api is set to get data, save data and delete data. You can call these apis from your client side application, which you’ve put in _public file.


Now your Node server is ready. You just run node server.js in your project folder from your terminal or command prompt, and your http://localhost:8080/ will serve you index.html file, placed in _public folder. You client side operation will work perfect and all your api calls will be up and running. You can get the source code from the download link below.