A simple Node Static file server with just the node.js built in modules.
When working with many node projects I often run into a situation in which I need to just set up a simple static web sever, often purely for the sake of serving a path over the http rather than file protocol in a web browser. There are many npm packages such as node-static that can be used to pull this off, but I often find myself just working out a simple solution using the built in http module in node itself. It can be a bit time consuming to do this though, and when it comes to starting a more serious production app it might be better to use a well supported framework such as express to make quick work of this and much more. However in this post I will be using just plain old native javaScript in node.js to create a simple node static file server.
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.
|
|
And the script will serve static files that I have in a public folder localed at the current working path where I have the script, or a hard coded default public folder location. I could also install the script globally, but for now I just want to make a script that is closely tied to the project folder. There is looking into what the options are when it comes to popular frameworks, and what features they have to make quick work of this sort of thing and move on with what really matters with your project. However in some situations I might want to have some kind of drop in vanilla javaScript solution and not make use of some kind of user space library or framework.
1.1 - Using express static if you do not mind using express
If you do not mind adding express to a projects list of dependencies, of it is is a part of it all ready, this can be done very quickly with the express static built in middle ware. By doing so it will not be a native vanilla javaScript solution for this, but express is a great project for getting going with a serious back end project. Also when it comes to using express there is a great official middle ware called serve index that will go beyond what is built in with additional features that will generate an index for paths to which there is no index html file.
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
Writing these kinds of files now and then is fun, and every now and then I come around to make a more robust version of this sort of file. Of course nether of these static sever script files are battle tested, but it seems to work fine for me in most cases when it just comes to playing with some kind of javaScript project that needs to be severed up over http. That is where I often use this kind of file, so it makes sense to have a post on this topic, and a github repo to hold the source code. I hate wasting time coming up with new solutions for problems that can be fixed more or less permanently. So whenever I want to use this kind of script I can just copy what I have work out here.