Creating a Basic CRUD API with Node.js, Express, and MongoDB for Beginners

Posted by

Building Restful CRUD API with Node.js, Express and MongoDB | FOR BEGINNERS

Building Restful CRUD API with Node.js, Express and MongoDB | FOR BEGINNERS

Are you a beginner in web development and looking to build a Restful CRUD API using Node.js, Express, and MongoDB? You’ve come to the right place! In this article, we’ll walk you through the steps to create a simple Restful CRUD API.

Step 1: Set Up Your Environment

First, you’ll need to have Node.js and MongoDB installed on your machine. You can download and install Node.js from their official website, and MongoDB from their website as well. Once you have them installed, you’re ready to move on to the next step.

Step 2: Create a New Node.js Project

Open up your terminal or command prompt and navigate to the directory where you want to create your new Node.js project. Run the following command to create a new Node.js project:


npm init

Follow the prompts to set up your project. Once that’s done, you’ll have a new package.json file in your project directory.

Step 3: Install Express and Mongoose

Express is a popular web application framework for Node.js, and Mongoose is an Object Data Modeling (ODM) library for MongoDB. Run the following command to install them as dependencies for your project:


npm install express mongoose

Step 4: Create a Server with Express

Create a new file called server.js in your project directory. In this file, you’ll create a simple Express server to handle incoming HTTP requests:


// server.js
const express = require('express');
const app = express();

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

Step 5: Set Up MongoDB Connection

Next, you’ll need to set up a connection to your MongoDB database. You can do this by adding the following code to your server.js file:


// server.js
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/my_database', {useNewUrlParser: true, useUnifiedTopology: true}, () => {
console.log('Connected to MongoDB');
});

Step 6: Define Your Data Model

Create a new file called model.js in your project directory. In this file, you’ll define the schema for your data model using Mongoose:


// model.js
const mongoose = require('mongoose');

const Schema = mongoose.Schema;
const mySchema = new Schema({
name: String,
age: Number
});

const MyModel = mongoose.model('MyModel', mySchema);

module.exports = MyModel;

Step 7: Create CRUD Endpoints

In your server.js file, create CRUD endpoints using Express to perform operations on your data model:


// server.js
const MyModel = require('./model');

// Create
app.post('/api/myModels', (req, res) => {
const newModel = new MyModel(req.body);
newModel.save((err, data) => {
if (err) {
res.status(500).send(err);
} else {
res.status(201).send(data);
}
});
});

// Read
app.get('/api/myModels', (req, res) => {
MyModel.find({}, (err, data) => {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send(data);
}
});
});

// Update
app.put('/api/myModels/:id', (req, res) => {
MyModel.findByIdAndUpdate(req.params.id, req.body, {new: true}, (err, data) => {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send(data);
}
});
});

// Delete
app.delete('/api/myModels/:id', (req, res) => {
MyModel.findByIdAndRemove(req.params.id, (err) => {
if (err) {
res.status(500).send(err);
} else {
res.status(204).send();
}
});
});

Step 8: Test Your API

Congratulations! You’ve now created a Restful CRUD API using Node.js, Express, and MongoDB. You can test your API using tools like Postman to make HTTP requests to the endpoints you’ve created.

That’s it! You’ve now learned how to build a simple Restful CRUD API with Node.js, Express, and MongoDB. Happy coding!