DEV Community

ncutixavier
ncutixavier

Posted on • Edited on

πŸ“ CRUD Operation with Node.js, Express & MongoDB

This guide teaches how to build a simple Blog Article CRUD API using:

  • Node.js – runtime to run JavaScript on the server
  • Express – web framework for building APIs
  • MongoDB – NoSQL database
  • Mongoose – MongoDB ODM (object-document mapper)

πŸ› οΈ Step 1: Setup the Project

1. Create a folder and initialize npm:

mkdir blog-crud && cd blog-crud npm init -y 
Enter fullscreen mode Exit fullscreen mode

2. Install required packages:

npm install express mongoose body-parser 
Enter fullscreen mode Exit fullscreen mode
  • express: Web framework
  • mongoose: Connect and interact with MongoDB
  • body-parser: Parse incoming JSON request bodies

πŸ“ Step 2: Project Structure

Organize your project files like this:

blog-crud/ β”œβ”€β”€ models/ β”‚ └── Article.js # Mongoose schema β”œβ”€β”€ routes/ β”‚ └── articles.js # Route handlers β”œβ”€β”€ server.js # Main app entry β”œβ”€β”€ package.json 
Enter fullscreen mode Exit fullscreen mode

πŸš€ Step 3: Create the Express Server (server.js)

const express = require('express'); const mongoose = require('mongoose'); const bodyParser = require('body-parser'); const articleRoutes = require('./routes/articles'); const app = express(); // Middleware to parse JSON app.use(bodyParser.json()); // Route prefix: All article routes will start with /api/articles app.use('/api/articles', articleRoutes); // Connect to MongoDB mongoose.connect('mongodb://localhost:27017/blog_crud', { useNewUrlParser: true, useUnifiedTopology: true }).then(() => console.log('βœ… MongoDB Connected')) .catch(err => console.error('❌ DB Connection Error:', err)); // Start the server app.listen(3000, () => console.log('πŸš€ Server running at http://localhost:3000')); 
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • This is your main app file.
  • It sets up Express, connects to MongoDB, and loads article routes.

πŸ“¦ Step 4: Create the Article Schema (models/Article.js)

const mongoose = require('mongoose'); const articleSchema = new mongoose.Schema({ title: String, content: String, author: String, createdAt: { type: Date, default: Date.now } }); module.exports = mongoose.model('Article', articleSchema); 
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Defines the structure of each Blog Article in the database.
  • Includes fields like title, content, author, and createdAt.

πŸ” Step 5: Create CRUD Routes (routes/articles.js)

const express = require('express'); const router = express.Router(); const Article = require('../models/Article'); 
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 1. Create a new Article (POST /api/articles)

router.post('/', async (req, res) => { try { const article = await Article.create(req.body); res.status(201).json(article); } catch (err) { res.status(400).json({ error: err.message }); } }); 
Enter fullscreen mode Exit fullscreen mode

Accepts JSON { title, content, author } and stores it in the database.


πŸ”Ή 2. Read All Articles (GET /api/articles)

router.get('/', async (req, res) => { try { const articles = await Article.find(); res.json(articles); } catch (err) { res.status(500).json({ error: err.message }); } }); 
Enter fullscreen mode Exit fullscreen mode

Returns all articles stored in the database.


πŸ”Ή 3. Read a Single Article by ID (GET /api/articles/:id)

router.get('/:id', async (req, res) => { try { const article = await Article.findById(req.params.id); if (!article) return res.status(404).json({ message: 'Not found' }); res.json(article); } catch (err) { res.status(500).json({ error: err.message }); } }); 
Enter fullscreen mode Exit fullscreen mode

Fetches a specific article using its ID.


πŸ”Ή 4. Update an Article (PUT /api/articles/:id)

router.put('/:id', async (req, res) => { try { const updated = await Article.findByIdAndUpdate( req.params.id, req.body, { new: true } // return the updated document ); if (!updated) return res.status(404).json({ message: 'Not found' }); res.json(updated); } catch (err) { res.status(400).json({ error: err.message }); } }); 
Enter fullscreen mode Exit fullscreen mode

Edits article details like title or content.


πŸ”Ή 5. Delete an Article (DELETE /api/articles/:id)

router.delete('/:id', async (req, res) => { try { const deleted = await Article.findByIdAndDelete(req.params.id); if (!deleted) return res.status(404).json({ message: 'Not found' }); res.json({ message: 'βœ… Article deleted' }); } catch (err) { res.status(500).json({ error: err.message }); } }); 
Enter fullscreen mode Exit fullscreen mode

Deletes the selected article from the database.


Finally routes/articles.js

const express = require('express'); const router = express.Router(); const Article = require('../models/Article'); // βœ… Create an article router.post('/', async (req, res) => { try { const article = await Article.create(req.body); res.status(201).json(article); } catch (err) { res.status(400).json({ error: err.message }); } }); // πŸ“– Get all articles router.get('/', async (req, res) => { try { const articles = await Article.find(); res.json(articles); } catch (err) { res.status(500).json({ error: err.message }); } }); // πŸ“„ Get a single article by ID router.get('/:id', async (req, res) => { try { const article = await Article.findById(req.params.id); if (!article) return res.status(404).json({ message: 'Not found' }); res.json(article); } catch (err) { res.status(500).json({ error: err.message }); } }); // ✏️ Update an article router.put('/:id', async (req, res) => { try { const updated = await Article.findByIdAndUpdate(req.params.id, req.body, { new: true }); if (!updated) return res.status(404).json({ message: 'Not found' }); res.json(updated); } catch (err) { res.status(400).json({ error: err.message }); } }); // ❌ Delete an article router.delete('/:id', async (req, res) => { try { const deleted = await Article.findByIdAndDelete(req.params.id); if (!deleted) return res.status(404).json({ message: 'Not found' }); res.json({ message: 'Article deleted' }); } catch (err) { res.status(500).json({ error: err.message }); } }); module.exports = router; 
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Step 6: Testing the API

Use tools like Postman, Insomnia, or curl:

  • Create:
 curl -X POST http://localhost:3000/api/articles \ -H "Content-Type: application/json" \ -d '{"title": "My First Blog", "content": "Hello world!", "author": "Xavier"}' 
Enter fullscreen mode Exit fullscreen mode
  • Read All:
 curl http://localhost:3000/api/articles 
Enter fullscreen mode Exit fullscreen mode
  • Read One:
 curl http://localhost:3000/api/articles/<article_id> 
Enter fullscreen mode Exit fullscreen mode
  • Update:
 curl -X PUT http://localhost:3000/api/articles/<article_id> \ -H "Content-Type: application/json" \ -d '{"title": "Updated Title"}' 
Enter fullscreen mode Exit fullscreen mode
  • Delete:
 curl -X DELETE http://localhost:3000/api/articles/<article_id> 
Enter fullscreen mode Exit fullscreen mode

βœ… Final Notes

Top comments (0)