Create your own server with Node.JS
Most of the tech-inclined people know Javascript, the browser-interpreted language that is used on almost every website on the Web. You may already know that JS has a lot of frameworks, enabling you to literally program your way into creating a website, but did you know that you can program your server on that same language? Yes, Javascript is full stack now, something we call a MEAN stack. This article will teach you how to create a simple web server using MongoDB, Express and Node.
Step 1 —Setting up
The first step is to install Node on your machine, going to the Node.JS downloading page and choosing the file that suits your operating system. After installation, create a new folder/directory and start a Node project. You can use your preferred bash terminal (Windows has it too, you know?) and simply type:
npm init
It will ask you some questions about your project but in all honesty I normally just click Enter on every one of them.
Furthermore we want to install our dependecies, the Express framework and Body Parser, for which we’ll use our bash again:
npm install --save express body-parser mongoose
In this project we want to create a Model-View-Controller architecture to prevent from attacks such as SQL Injection. So we create our folders to each group of files by using our bash again:
mkdir models views controllers routes
Step 2 — Files
Now that we have a fresh Node project with MVC architecture, lets start by creating the main file:
touch index.js
And opening that same file with your text editor of choice. On that document we are going to write the following: