We have installed nodeJs and initialized the package.json file.
Since our main file is an index.js let’s start with adding start and dev scripts:
Also, we will be using ES6 syntax so let’s make the type as modular.
We will be now installing some necessary dependencies and will install more as and when needed.
npm i nodemon mongoose cors body-parser bcryptjs dotenv express
Type this in the terminal to install these packages
Nodemon -We will be needing a nodemon for continuous development and integration. By using it we won’t have to stop and run the app, again and again, we can just make changes and save them to verify.
Mongoose- We will be using MongoDB so will need mongoose for defining the schema and creating tables.
Cors- Since I am working on the backend the frontend could be hosted on any platform so we will use cors to enable cross-platform.
Express provides methods to specify what function is called for a particular HTTP verb ( GET, POST, SET, etc.) and URL pattern (“Route”), and methods to specify what template (“view”) engine is used, where template files are located, and what template to use to render a response.
body-parser allows express to read the body and then parse that into a JSON object that we can understand. It parses the HTTP request body.
dotenv allows you to separate secrets from your source code. This is useful in a collaborative environment (e.g., work, or open-source) where you may not want to share your database login credentials with other people. Instead, you can share the source code while allowing other people to create their own.
I will be using authentications and users so I will need bcrypt to encrypt the passwords bcrypt library has been deprecated so we will be using bcryptjs for password hashing.
You will have now a node modules folder and a package-lock.json file created.
To prevent node modules from getting pushed to GitHub create a .gitignore file and add node modules to it.
Let’s create a file structure now!
This is what file structure should look like
controllers — in there we have main logic code
models — the schema for tables
routes- sub-paths for different logics
middleware- for a function to be executed before the main logic
You will understand better as we move forward!