NodeJS Static File Server

Create NodeJS – ExpressJS static file server in a minute

It’s pretty straight-forward tutorial for anybody to understand about to create NodeJS static file server. Suppose, you want to run your AngularJS static application in your local PC with pretty URLs and other features. You may need to create a stand-alone static file server.

So here you can do that with NodeJS and Express.

– Just create a package.json file in your application root. and write the following json configuration there.

  "scripts": {
    "server": "node static-server.js"
  "dependencies": {
    "express": "^4.14.0",
    "node-static": "^0.7.7",
    "path": "^0.12.7"

And after that create a JS file name static-server.js with the following code where ExpressJS will serve you static files.

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/public'));

app.get('*', function(request, response, next) {
    //__dirname + '/public' is your main file location of a
    response.sendfile(__dirname + '/public');

    //if you want to run AngularJS app with html5Mode(true)
    //then you have to run server via index.html
    //response.sendfile(__dirname + '/public' + '/index.html');
    //Write your own angularJS index file location

app.listen(8081, function(){
    console.log('Starting server at http://localhost:8081')

Now you are all set. Now install all nodejs dependency libraries by npm install command and then run ‘npm run server’ command to start your static server. After successfully started (can see in console) you can visit your static web server from http://localhost:8081 (you can change port as your wish). I think this will help you and your team.

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.

Sharing is Awesome, Do It!

Share this post with your friends