Create a CRUD API in Node.JS and Express - Code sample A sample project illustrating how to create a basic CRUD API in Node.JS using the Express framework

Crea un'API CRUD in Node.JS ed Express - Esempio di codice

Creating a basic CRUD (Create, Read, Update, Delete) API in Node.js can be accomplished using the Express framework, which simplifies the process of building web applications and APIs. This guide provides a step-by-step walkthrough for creating a basic CRUD API, showcasing best practices and practical coding techniques.

We created this API to help new developers and those looking to expand their skills in building modern web applications using Node.js and Express. Whether you're just starting out with backend development or want to strengthen your understanding of Express.js, this example will serve as a simple yet powerful foundation.

Throughout this guide, you'll learn how to set up a Node.js project, create API routes, and integrate them with a database to handle basic data operations. By the end, you'll have a fully functional API that you can use as a starting point for your own projects or to deepen your understanding of backend technologies.

Prerequisites

  • Node.js installed on your machine. You can download it from Node.js official website.
  • npm (Node Package Manager), which comes with Node.js.
  • A text editor or an IDE (like Visual Studio Code).

1: Set up the Project

Create a new directory for your project and navigate into it:

Initialize a new Node.js project:

Install Express and other required packages:

2: Create the Basic Server

Create a new file named server.js in your project folder.

Add the following code to server.js:

3: Run the Server

To start your server, run the following command in your terminal:

You should see the message indicating that the server is running.

4: Test the API

You can use tools like Postman or cURL to interact with your API.

Create a User

  • Method: POST
  • URL: http://localhost:3000/users
  • Body: { "id": "1", "name": "John Doe", "email": "[email protected]" }

Read All Users

  • Method: GET
  • URL: http://localhost:3000/users

Read a User by ID

  • Method: GET
  • URL: http://localhost:3000/users/1

Update a User

  • Method: PUT
  • URL: http://localhost:3000/users/1
  • Body: { "name": "John Smith" }

Delete a User

Method: DELETE
URL: http://localhost:3000/users/1

Conclusion

This is a simple CRUD API using Node.js and Express. It uses an in-memory array to store user data, which will be lost when the server restarts. For a production application, consider using a database (like MongoDB, PostgreSQL, etc.) to persist data.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.