Installation of Package.json
- Create a folder named backend folder.
- Inside backend folder create file named server.js.
- Inside server.js file write the following code: “console.log(‘hello World!’)”
- Then in command prompt write the given command: npm init
- In response of above command package.json will be installed; after filling the details shown in the figure below.
gitignore File
- Create a file named .gitignore in the same backend folder.
- The .gitignore file tells Git which files or directories to ignore — meaning they won’t be tracked, committed, or pushed to a Git repository.
- This file avoid committing sensitive dataExample: API keys, passwords, .env files.
- In our case, we write node_modules, and gitignore inside .gitignore file for avoid to commit to git repository.
Installation of Necessary Tools
- The packages includes express, env, mongoose and colors are installed by using the command: npm i express dotenv mongoose colors.
- For dev dependence install nodemon tool by using the given command. npm i –D nodemon
- nodemon is a utility that monitors for changes in your Node.js application and automatically restarts the server when files are modified. It’s super handy for development because you don’t have to manually stop and start your server every time you make a change.
Adding Code in Package.json
- Go to package.json file and add server script for the launching or running of the application.
- Nodemon automatically detect the changes in code and re-run the application for experiences up-to-date application functionality and interfaces.
Code of Server.js File
- In server.js file write below snippet of code.
- In this code we import two packages express and dotenv.
- Express:
- Express is a lightweight, fast, and flexible web framework for Node.js used to build web servers and APIs.
- It provides a simple, minimal framework to handle routing, middleware, HTTP requests, and more.
Code of .env File
- Create .env file inside backend folder.
- Add given snippet of code in it.
- In Node.js with Express, you often need to run your app differently depending on whether you're in development or production mode (e.g., logging more in dev, hiding errors in prod, etc.).
Code of Controller.js File
- Now, create controller folder inside backend folder.
- Inside controller folder; create goalController.js file.
- Install package ‘express-async-handler’ by using the command: npm i express-async-handler
- Import ‘express-async-handler’ package in goalController.js file.
- The snippet of code for route handling the get goals request is as:
- It is also part of a restful API for managing goals.
- The snippet code for route handler than set goal is as:
- The snippet code for route handler than update goal is as:
- The snippet of code for route handling the delete goal is as:
Code of goalRouters.js File
- Create a goalRouters.js file inside backend folder.
- Write this snippet of code in goalRouters.js file.
- In this code, import express package.
- Import router handler functions i.e. getGoal, setGoal, updateGoal and deleteGoal.
- Then use Router function of express for defining routers.
Finally, Test the above API's by using postman tool.


Comments