How I built this site with NodeJs, Express, EJS and MongoDB

by Jan Gabriel | 24 Jan 2021

last edited: 24 Jan 2021

WebDev NodeJs Express EJS MongoDB

I must admit that I am not a web developer and before I started to build this site I didn't really know much about this field. What I can say, is that the web development community is awesome. There is really a lot of people that share tutorials, support each other and teach on the subject for free or at least for a reasonable fee (e.g. on Udemy). In this post, I'll share my own lessons learned and an overview of the decisions I made in building this site.

I'll discuss the following three points:

  • Choosing a Web Stack;
  • Getting a domain name; and
  • Hosting your web application

Choosing a Web Stack

The first time I heard of a web stack, I had no idea what it was. I only knew that you can get online very quickly if you used a Content Management System i.e. a CMS. You would have heard of some of the more popular ones such as WordPress, Wix, Drupal and a lot of others as well. I'd prefer to use a CMS when you need to get something online quickly, and when whoever has to manage the site, does not have a ton of technical knowledge. They provide templates to get started and on which you can build your site and even edit most of the content in an editor. This means you don't have to touch HTML, CSS & JS if you don't need to.

A web stack is basically the technology stack you use to enable users to interact with a static or dynamic web application. This is all the way from the human interface (frontend) in the browser, to the server running database queries and business functions a.k.a. the backend. This is where I got a lot of analysis paralysis. There is quite a lot of tech stacks/frameworks to choose from. You also need to decide on server-side or client-side rendering, server or serverless, and a couple of other things. It really depends on what you want to achieve, but I'll stick to the web stack I've chosen for this site (a blog site).

First, you need an application to run on an operating system, which enables network communication. On top of that, you need a framework, which will handle the various protocols used in web applications. I chose the following:

The combination of NodeJs and Express is widely used and enables server-side Javascript to run your web server. This is great because now everything is JavaScript!

Secondly, I decided to use a templating engine to ensure I keep my code DRY. I really like Handlebars, but I chose EJS at the end of the day.

  • Template Engine: EJS

This was mainly due to the fact that a lot of the tutorials I watched on youtube used EJS. The templating engine allows you to specify the HTML, for example, your page (like this one), and then fill it with the selected blog post retrieved from your database. EJS enables you to write Javascript inside the HTML template to insert the dynamic content on the server-side and then respond to a client with only what you need to see. This is called server-side rendering.

Lastly, you'll need a database to store posts, comments, and anything really. I decided to go with MongoDB for the same reason I decided to choose EJS.

The NodeJs MongoDB driver really makes the database queries super easy. The documentation is also very well written and maintained.

That is all you need in order to run a web application like this one. There is a ton of content out there, which will help you get a NodeJs application up and running with Express, EJS, and MongoDB. I'd recommend doing your own research before choosing a Web Stack specific to your target application.

Getting a domain name

Once you've built something you want to put online, you definitely need a domain name. This part is really quite easy and frustrating at the same time. Just head over to a domain name provider and choose something that will fit your application. I used Namecheap, which was easy to use, and it was only a matter of finding something that is still available for the taking.

Hosting your web application

If you want to build a static site. You can always host it for free on github pages, or if you decide to go serverless try Vercel with NextJs. In my case, I wanted to run C++ and Python applications in the backend, which are always connected. Thus, I needed a Virtual Machine (VM). I decided to use DigitalOcean. They really give you as much control as you can ask for.

Their pricing options are very good and they make the VM setup super easy with their how-to-get-started guide. Remember to follow their guide and use a Nginx instance as a reverse proxy to connect your NodeJs app to the net. Also, head over to Let's Encrypt for a free SSL certificate.

In the end, your decisions for developing a web application really depend on what you want to do with it. I loved the journey of learning about web development and getting my first web app up and running! I must say, I have much respect for the people doing this full-time. It is a very cool industry and my curiosity will definitely lead me to build more stuff online.

Feel free to leave any comments/questions through the contact page and I'll get back to you.

Happy coding.