CRUD operation using Node.js and Express.js

Jenil Gajjar
3 min readSep 3, 2022

--

In this medium article, I will show you how to perform basic CRUD operations with the help of Node.js and Express.js. You will also get an idea of project setup in Node.

Project Setup

Step-1

  • To create a new project run the below command where you made your project directory.
  • It is used to set up a new or existing npm package.
npm init

Step-2

  • Now install below two packages.
npm install express body-parser
  • Express: Express.js is a free and open-source web application framework for Node.js. It is used for designing and building web applications quickly and easily.
  • Body Parser: Express body-parser is an npm library that processes data sent through an HTTP request body.

Step-3

npm install -D nodemon
  • Nodemon: Nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.
  • -D is shorthand for devDependencies. Dev dependencies are modules which are only required during development whereas dependencies are needed at runtime.

Step-4

  • In the package.json file, write the below command inside the scripts object. So, when you run the server using npm start it will run the index.js file using nodemon.
"scripts": {
"start": "nodemon index.js"
}

Step-5

  • Run the server 😀
npm start

The CRUD

Now, we will learn how to perform CRUD operations. CRUD stands for Create, Read, Update and Delete.

Step-1

  • Create an index.js file in the root directory of the project.

Step-2

  • Create a routes folder inside the root directory and inside this folder create a file by the name items.js.
  • In this file, we will be creating all our routes(GET, POST, UPDATE, DELETE).
  • Now, inside the index.js file import the items.js file.

Step-3

  • Now we will create our remaining routes inside the items.js file.
  • Firstly, create an array by the name itemData in the items.js file.
let itemData = [
{
"name": "Waffles",
"toppings": "strawberry",
"price": 200
}
];
  • Create a post route as shown below.
  • uuidv4(): It is a universally unique identifier that is generated using random numbers. It is an npm package that provides a secure way to generate cryptographically strong unique identifiers with Node.js.
npm install uuidv4
  • You can now test the API using VS Code’s thunder client or postman.
  • After adding a new item to the array run the GET method API. It will show you two items stored in the itemData array.
  • Similarly, we will create the remaining routes as well.
  • To get a particular item from the array, we will create a get method API to get an item through a unique ID.
  • Likewise, we will create delete and update route to delete and update a particular item through a unique ID.

Step-4

  • In this step, we will restructure our code.
  • Create a folder name controllers and an items.js file in the folder, where we can separate our routes.
  • So our items.js which is inside the routes folder will look like this.
  • So, in this article, we learned about four basic CRUD operations using Node.js. We came to know about three npm packages that are express, body-parser and uuidv4.
  • I hope you find this article interesting and learned something useful. Keep Clapping.👏👏

--

--

Jenil Gajjar
Jenil Gajjar

Written by Jenil Gajjar

Cyber Security 🛡️ | Eager to learn new technologies 🤓 | Self Learner 💪

Responses (1)