Clean Code hochulshin.com

Nodejs - Making a simple file server

2015-08-17

I am reading the book, Pro Angular.js and practicing the samples on the book. However, many samples have been already deprecated. The static file server using connect.js is as well. Furthermore, the hapi file server that I implemented has been deprecated too. Therefore, I had to find the alternatives.

serve-static in expressjs

  • Make a project folder

  • Run npm init to initialize the node project.

  • Install finalhandler and serve-static npms.

npm init; npm install —save-dev final handler serve-static

  • Make a file - server.js - in the project root folder.

  • Add the following contents to the server.js file. The file path with the serveStatic function should be changed for your file path.

var http = require('http');

var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');

var serve = serveStatic("./); 

var server = http.createServer(function(req, res) {
  var done = finalhandler(req, res);
  serve(req, res, done);
});

server.listen(3000);
  • Let’s run the file server.

node server.js

  • Test with localhost:3000/filename on a Web browser.

Comments