A simple Node Static file server with just the node.js built in modules.
1 - Node Static Basics
So the general idea of a node static server is that I have some plain old static files in a public folder and I just want to serve them up over the http protocol on my local computer. Maybe what I am working on will be deployed to a site elsewhere at a latter time, or maybe I am just running into some problems that will pop up when doing everything over the file protocol. Whatever the case may be in some situations I might find myself in a situation in which I need to host a folder on the local file system of a computer and it will just need to respond to get requests and that is it.
So I just need a script that I can call from node like this.
1.1 - Using express static if you do not mind using express
1.2 - The source code examples in this post are on my github
I have a github repository set up for this simple server file example. If you feel as though there is something in the source code that warrants a need for a pull request that would be where to do so. In some cases the source code there may also be a little more up to date compared to what I have wrote about in this post also.
2 - New static sever solution example that uses promises
I first wrote this post back in December of 2017, the old script still works okay, but as of this writing I have made at least one new revision of this simple static sever file. The main goal was to just have a more fine grain form of file where I break things down a little more with various helper methods rather than having a file where there are a lot of nested calls of file system methods. So then in this from of the file I am now using promises, making use of the promsiify method of the built in util module to make sure that the use of a file system method will return a promise on older versions of node where that is not the standard.
3 - Old static sever solution
So here is a basic node static file server file that I worked out in the old form of the file when I first wrote this post back in 2017. Here I just placed this code in a file called server.js in the root path of the project folder. In the project folder I also have a public folder and in there I have a index html file at the root of the project folder.
By default if I do not give any arguments when calling the file, the script will assume that there is a public folder in the current working folder when the script is called with node. In many cases I might want to give an absolute path to the public folder as the first argument. I can also set a port for the script to listen on as the second argument, if not it will default to 8080.
I use the file system module of nodejs to check the stats of a file that is being requested. If the file is there then it will be sent to the client, if the file is not there then a 500 status will result and the request will be ended. If the file is there the file system module read file method will be used to read and send the contents of the file.
So of course this solution just handles GET requests which works fine in most situations. In the event that a path is given such as ‘/‘ then ‘/index.html’ is assumed. In addition in the event of any kind of error the request is just ended, and I do not serve any kind of 404 page.
4 - Conclusion