tl;dr — This is the first article of the Node is Simple article series. In this article series, I will be discussing how to create a simple and secure NodeJS, Express, MongoDB web application.
- Setting up a basic NodeJS, Express web app with SSL/TLS.
- Setting up ESLint in your favorite editor or IDE.
- Adding MongoDB as the database.
- Creating basic CRUD endpoints and testing with Postman.
- Uploading files and view them using MongoDB GridFS.
- Creating custom middleware with Express.
- Add logging for the web application.
- Securing endpoints with JWT authentication.
- Validating the input using @Hapi/Joi.
- Adding an OpenAPI Swagger Documentation.
- Caching the responses with Redis.
- Load balancing with PM2.
- Testing the API using Chai, Mocha.
- Create a CI/CD pipeline.
- Deploying to your favorite platform.
Well if you have done all of these things with your web application, it would be awesome. Since I learned all of these the hard way, I want to share them with all of you. Because sharing is caring 😇.
Since this tutorial is a series I won’t be making this a long; boring to read one. In this first article, I will describe how to set up a simple NodeJS and Express app with SSL/TLS. Just that, nothing else.
On your feet soldier, we are starting.
First of all, let’s create the folder structure of our web application.
As shown in figure 1, you need to create the folders and the index.js file. If you use Linux or, Git Bash on Windows, let’s make a simple script to do this. So you won’t have to do this again when you need to create another application. (We are so lazy aren’t we? 😂)
Let’s go through it again.
First, you need to create a folder for your project and run npm init and initialize your application. With this, you can specify a great name to your project, your license of preference, and many more. Ah, I just forgot. You need to check if you have the current version of Node and NPM installed in your machine. I hope you know how to install NodeJS on your computer. If not please refer to the following links.
How to Download & Install Node.js — NPM on Windows
To start building your Node.js applications, the first step is the installation of the node.js framework. The Node.js…
How To Install Node.js on Ubuntu 18.04 | DigitalOcean
After doing that just run this bootstrap.sh bash script inside your project folder and it will create a simple Node, Express application. So simple right?
After creating the necessary folder structure let’s set up the Express application. Let’s go to the index.js file on the root folder and add these lines.
Don’t run this yet, since we haven’t done anything yet. I know this is a bit too much just bear with me, I’ll explain everything. What we are doing here is, first we create an HTTP server and then we create an HTTPS server. Then we redirect all the HTTP traffic to the HTTPS server. First of all, we need to create certificates to use inside the HTTPS server. It is a little bit of pain, but it’ll worth it. This is a good resource about creating SSL certificates for your Node application.
How to Use SSL/TLS with Node.js — SitePoint
In 2020, there’s no reason for your website not to use HTTPS. Visitors expect it, Google uses it as a ranking factor…
I am a little bit of a lazy person so I’ll just generate server.cert and server.key files using this link.
Self-Signed Certificate Generator
Self-signed ssl certificates can be used to set up temporary ssl servers. You can use it for test and development…
Inside the server name input, you just have to provide your domain name. For this purpose, I’ll use the localhost as the domain name.
After generating the *.cert and *.key files copy them to the /config folder. And rename them to server.cert and server.key.
Now let’s import the certificates and make them useful. Inside the /config folder create the file index.js and add these lines.
Not so simple after all right? Well, let’s see.
Now that we have set up the certificates, let’s create a simple controller (endpoint) to our application and check that out. First, let’s got to the /controllers folder and create an index.js file. Inside that add the following lines.
asyncWrapper what is that???
Let me tell you about that. Async Wrapper is a wrapper function that will catch all the errors happen inside your code and returns them to the error handling middleware. I’ll explain this a little more when I am discussing Express middleware. Now let’s create this infamous asyncWrapper. Go to the /utilities folder and create the two following files.
async.wrapper.d.ts (Async wrapper type definition file)
We have done it, folks, we have done it. Now let’s check this beautiful Express app at work. Let’s run the web application first. Let’s go to the project folder and in the terminal (or Git Bash on Windows) run the following line.
After that let’s fire up your favorite browser (mine is Chrome 😁) and hit the endpoint at,
Don’t worry if your browser says it is not secure to go to this URL. You trust yourself, don’t you? So let’s accept the risk and go ahead.
Now if you can see something like this,
You have successfully created a NodeJS, Express application. Also since we have set up an HTTP server, you can try this URL too.
If you go to this URL, you’ll see that you’ll be redirected to
Now that’s the magic with our Express application. Awesome right? So this is it for this tutorial. In the next tutorial, I’ll tell you all about adding MongoDB as the database.
You can’t perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
This is the GitHub repo for this tutorial and I’ll update this repo as the tutorial grows. Check for the commit messages for the snapshot of the repo for a particular tutorial. If you have any queries, don’t forget to hit me up on any social media as shown on my website.
About Myself I am a person who seeks solutions for questions and to understand the inner workings of the world. The…
So until we meet again with part two of this tutorial series, happy coding…