We are going to do a static file server in Node.js. This web server is going to respond with the content of the file in a given path. While we are doing this exercise we are going to cover more about
http module. Also, use some utilities from other core modules such as
Node’s HTTP module is versatile. You can use it as a client, to grab content from websites or as a server. We are going to use it server files from our file system.
If you are familiar with Ruby or Python or http-server package. It’s the equivalent of this:
# python HTTP server
Let’s do our own. It’s not that hard.
One of the simplest servers that you can create in Node, looks like this:
const http = require('http');
To test it out, save the code in a file called
server.js and run:
Then open the browser on
http://localhost:9000 and you will see the “hello world!” message.
Let’s explain what’s going on in the code. We are using the function
http.createServer with a callback. This callback function is going to be called every time a client connects to the server. You can see that it takes two parameters:
The request contains the client’s information. For instance: requested URL, path, headers, HTTP method, and so forth.
The response object is used to reply to the client. You can set what you want to send back to the client. For instance, data, headers, etc.
Finally, the listening part. It allows you to set the port that you want your server to run on. In this case, we are using
Let’s now proceed to do the static web server. We want to parse the URL path and get the file matching that path. For instance, if we get a request like
localhost:9000/example/server.js. We want to look for a file in
Browsers don’t rely on the extension to render a file. Instead, they use the header
Content-type. For instance, if we serve an HTML file with a content type
text/plain it will show the HTML code (plain text). But, if you use a content type
text/html then it will render the HTML as such.
For now, we can infer the file content type based on the file extension. The content types are represented in MIME formmat. MIME stands for Multipurpose Internet Mail Extensions. You can see the MIME types according to file extentions in the following code:
const http = require('http');
We are using Node.js core
path.parse libraries to get the extensions from the URL path. Similarly, we are using
url.parse to break down the
request.url into its components. Then, we extract the extension from the file. Finally, we use
fs.readFile to get the content from the file system. If any error occurs related to the file path, we return a 404 and otherwise return the file content.
Give it a try with:
# run server
For the first one, you will get a 200 OK response, while for the 2nd one you will get a 404 not found error, as expected.
You can also download the code from this repo and try out with the test files:
# Get Repository
In this post, we went through the basics about
http module to create a server. We talk about the MIME types and how the help the browser to render properly. Finally, we put all together to accomplish our static file server with Node.js!