Express.js tutorial with examples using CRUD operations
In this tutorial, we will walk through how to set up a simple Express.js server with CRUD (Create, Read, Update, Delete) operations. Express.js is a minimalist web framework for Node.js that makes it easy to build robust web applications and APIs.
We will build a basic Todo API with Express.js and use MongoDB as the database to store our todos. MongoDB is a NoSQL database that stores data in a flexible, JSON-like format.
Steps:
- Setting up Express.js
- Creating CRUD operations (Create, Read, Update, Delete)
- Connecting to MongoDB
- Testing the API with Postman
1. Setting up the Project
1.1. Install Node.js and Express
First, ensure that Node.js is installed on your machine. If not, download and install it from nodejs.org.
Now, create a new project directory and initialize the project:
This will create a package.json
file in your project directory.
Next, install Express.js and Mongoose (the MongoDB ODM for Node.js):
- express: Web framework for Node.js.
- mongoose: MongoDB ODM to manage data models and interact with MongoDB.
- body-parser: Middleware to parse incoming request bodies, especially for
POST
andPUT
requests.
1.2. Project Structure
Create the following folder structure for your project:
2. Connecting to MongoDB
2.1. MongoDB Setup
To use MongoDB, you need to have MongoDB installed locally or use a cloud service like MongoDB Atlas.
Install MongoDB locally or sign up for MongoDB Atlas and create a database.
Create a database called
todoapp
or use the default database.
2.2. Database Configuration (db.js
)
Create a config/db.js
file to handle MongoDB connection using Mongoose:
This code connects to a MongoDB instance running locally on port 27017
and uses the database todoapp
.
3. Creating a Todo Model
3.1. Define Todo Schema (todo.model.js
)
In the models
folder, create a file todo.model.js
. This will define the structure of our Todo data:
This schema defines:
title
: A string that is required.completed
: A boolean field with a default value offalse
.timestamps
: Automatically addscreatedAt
andupdatedAt
fields to the document.
4. Setting Up CRUD Routes
4.1. Define Routes for CRUD Operations (todo.routes.js
)
Create a routes
folder and inside it, create a file todo.routes.js
to handle the routes for Create, Read, Update, and Delete operations:
4.2. Use the Routes in the Main App (app.js
)
In the app.js
file, import the necessary modules, set up the server, and use the todo.routes.js
file for the routes.
5. Testing the API
5.1. Start the Server
Start the server by running the following command:
The server will be running at http://localhost:3000
.
5.2. Test with Postman or CURL
You can use Postman or CURL to test the CRUD API.
Create a Todo (POST request)
URL:
http://localhost:3000/api/todos
Body (JSON):
Get All Todos (GET request)
URL:
http://localhost:3000/api/todos
Get Todo by ID (GET request)
URL:
http://localhost:3000/api/todos/{id}
Update a Todo (PUT request)
URL:
http://localhost:3000/api/todos/{id}
Body (JSON):
Delete a Todo (DELETE request)
URL:
http://localhost:3000/api/todos/{id}
6. Conclusion
In this tutorial, we've created a simple CRUD API with Express.js and MongoDB using Mongoose. Here's a summary of what we've done:
- Set up Express.js and MongoDB.
- Created a Todo model using Mongoose.
- Built routes to handle CRUD operations for the Todo app.
- Tested the API using Postman or CURL.
This is a basic API that can be extended with more advanced features like authentication, validation, and error handling as you build more complex applications.
Comments
Post a Comment