How To Build Simple CRUD RESTful API With NodeJS, ExpressJS And MongoDB in 2022
--
Introduction
Node js is a popular backend nowadays! It’s fast and easy to write code. If you already know javascript, it can be easy to write node js. So we’re going to explore express js today with MongoDB for the database. We’ll create the rest API endpoints and implement CRUD operation for the to-do list. The example is quite basic, but we’re going to make a good project structure so that you can follow it for your other projects too! Let’s dive in –
Things I’m using
Vscode: I’ve used the vscode for the whole development!
Node js: I’ve used node js v14.8.0
MongoDB compass: I’ve used MongoDB v1.30.1 to see all changes in MongoDB graphically
Postman: We’ll use this one to test all our API’s
Packages:
- babel: For transpile the javascript into an older version
- cors: For the CORS setting
- dotenv: For reading the environment variables
- eslint: For enforcing a coding style
- express js: The node framework
- express-rate-limit: For limiting the API endpoint calls
- husky: For git pre-commit to hook for implementing eslint & prettier before committing
- joi: Used to validate the user inputs
- mongoose: MongoDB ORM for working with MongoDB
- morgan: For showing API endpoint details
- nodemon: For running the server, when we change a file
- prettier: For formatting the code
Project Setup
Basic Project Setup
We’ll start with npm init -y
on the folder to initialize the node and create the package.json
file. You can edit the package.json
file as you want! Now create an src
folder to write all our code in the folder. We’ll also make an index.js
file as a starter.
Setup the Express JS with babel
If we usually set up the nodejs with `npm init -y`, then we can’t use the latest features…