,

Creating a CRUD API with NodeJS, ExpressJs, and MongoDB

Posted by


CRUD API Using NodeJS ExpressJs and MongoDB

CRUD API is an essential part of building any web application that involves creating, reading, updating, and deleting data. In this article, we will learn how to build a CRUD API using NodeJS, ExpressJs, and MongoDB.

Setting Up the Environment

First, make sure you have Node.js and MongoDB installed on your machine. You can download and install Node.js from its official website and follow the installation guide for MongoDB from their official documentation.

Once you have Node.js and MongoDB installed, you can create a new directory for your project and run the following commands to set up a new Node.js project and install ExpressJs and MongoDB packages:

“`bash
mkdir crud-api
cd crud-api
npm init -y
npm install express mongoose
“`

Creating the Express App

Now, you can create a new file called `app.js` and set up a basic Express app with a connection to the MongoDB database:

“`javascript
const express = require(‘express’);
const mongoose = require(‘mongoose’);
const app = express();

mongoose.connect(‘mongodb://localhost/crud-api’, {
useNewUrlParser: true,
useUnifiedTopology: true
});

app.listen(3000, () => {
console.log(‘Server is running on port 3000’);
});
“`

Creating the Model

Next, you can create a new file called `user.js` to define a simple user model using Mongoose:

“`javascript
const mongoose = require(‘mongoose’);

const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
}
});

module.exports = mongoose.model(‘User’, userSchema);
“`

Implementing the CRUD Operations

Now, you can create routes in the `app.js` file to handle the CRUD operations for the user model:

“`javascript
const express = require(‘express’);
const mongoose = require(‘mongoose’);
const User = require(‘./user’);

const app = express();
mongoose.connect(‘mongodb://localhost/crud-api’, {
useNewUrlParser: true,
useUnifiedTopology: true
});

app.use(express.json());

app.get(‘/users’, async (req, res) => {
const users = await User.find();
res.send(users);
});

app.post(‘/users’, async (req, res) => {
const user = new User({
name: req.body.name,
email: req.body.email
});
await user.save();
res.send(user);
});

app.put(‘/users/:id’, async (req, res) => {
const user = await User.findById(req.params.id);
user.name = req.body.name;
user.email = req.body.email;
await user.save();
res.send(user);
});

app.delete(‘/users/:id’, async (req, res) => {
await User.findByIdAndDelete(req.params.id);
res.send({ message: ‘User deleted’ });
});

app.listen(3000, () => {
console.log(‘Server is running on port 3000’);
});
“`

Testing the CRUD Operations

Finally, you can test the CRUD API using a tool like Postman by sending HTTP requests to the endpoints you’ve defined in the `app.js` file. You can try creating, reading, updating, and deleting users to see the CRUD operations in action.

Congratulations! You have now successfully built a CRUD API using NodeJS, ExpressJs, and MongoDB.

Feel free to explore more advanced features and best practices for building APIs using these technologies to create robust and scalable applications.