It is very easy to set up a simple lightweight web server for a single page application.
Sometimes you don’t need a big server. It’s possible to use MAMP, but it seems like installing Apache,MySQL andPHP is too much. MAMP is terrific and I’ve used it for years. It is the cats pajamas when you need to connect to a database. Ten years ago that was the standard. Nowadays, it is very common to build a single page web application where all of the assets are static, data is pulled-in from a REST endpoint and all of the heavy lifting is done in the browser. A static Express server is a simple, easy and lightweight approach.
In this article, I will show you how to set up a static web server. The good news is that the steps are very easy to follow. The path and express modules are required first. After that, you need to set the port that the web-server will use. The next step is the express.static method. You want to serve static content from a folder. Most of the configuration work has been done in that one line of code.
Express can serve up static content from any folder, even if it is not in that folder. The static web folder is the folder you can specify. Your application code will be safe because any folder outside of the one you specify will be hidden. The details that express needs to serve are provided when you pass the express.static method to the use method denojs tutorial. The listen method is used to start the web server. We will take a closer look at the method.
I want to remind you that this article is about the specific times in which you need to serve static web assets. If you need a local web server but don’t want to use a static web server, using a Node / Express static web server may be the best option. In theory, you could deploy the code that is detailed in this article to your production server and it would work.
The code samples can be downloaded here: https://github.com/kevinchisholm/node-express-static-web-server
Example # 1 – package.json
We have the contents of the package. Nothing special going on here. The express module is the only dependency. I have set up the start command to execute the app.js file. We can type npm start in the terminal instead of typing in the app.js.
The Express is a static web server.
In example # 2, we have 15 lines of code and 25% of it is comments. The magic happens when we call the app.use method and pass it express.static. This tells Express that we want to keep a static folder. The path.join method is used to tell Express where static assets should be served from. The www folder is our case. The absolute path to the folder within which the current script is found, and the relative path to the www folder are both arguments passed to the path.join method.
Anything outside of your static folder is not open to the public. While the folder you specify is publically viewable, any folder that is a sibling or descendant is not. It does matter when working locally, but not in production. You don’t want your application code to be seen by the public. Any sensitive information in your application code, such as a secret key or other credentials, would not be made public. One of the key strengths of Express is the ability to define your public/static folder in one line of code, and to also protect all of the other folders by default.
Express does all of the heavy lifting
The word magic was used earlier. We both know that this is not magic, but it definitely feels like it. If you have ever created a web server manually, you know that it is easy and tedious once you get past Hello World. Express makes it easy to serve static assets.
There is a downside. Express doesn’t set the appropriate content type. This approach is meant to be very easy to set up a static web server. The web server works just fine, but keep in mind that the content-type headers for files will not be set. A simple static web server is probably not what you need, and you should consider a more robust approach. If you need a simple static web server, this article is what you need to get up and running.
There are many options when it comes to setting up a server. As a developer, you probably already have Node installed on your machine. In this case, you won’t need to install anything else. You can import the Express framework, write a few lines of code, and have a static web server. It’s not a server that you would use in production, but it can be used to solve the problem of serving web content quickly on your local machine. If you need to write logic that is moderately complex, then you might need something more advanced than what was discussed here. The approach should get you going in less than five minutes.