Google trends Public Holidays Coupon Code Code Compiler

A beginners guide to building a Node.js article API with Express and MongoDB


Sep 1, 2023

Node.js is a popular JavaScript runtime environment that can be used to build a variety of applications, including web applications, APIs, and microservices. Express is a lightweight web framework for Node.js that makes it easy to build RESTful APIs. MongoDB is a NoSQL database that is well-suited for storing large amounts of data.

1. Install Node.js and MongoDB. You can download Node.js from the Node.js website: https://nodejs.org/en/download/. For MongoDB, you can sign up for a free account on MongoDB Atlas: https://www.mongodb.com/cloud/atlas.

2. Create a new project folder. In your terminal, create a new folder for your project and navigate to it.

3. Install the Express framework. Run the following command to install Express:


npm install express

4. Create a new file called index.js. This will be the main file for your API.

5. Import the Express framework. In index.js, import the Express framework:


const express = require('express');

6. Create an Express app. Create an Express app and listen on port 3000:


const app = express();
app.listen(3000);

7. Connect to MongoDB. Import the MongoDB driver and connect to your MongoDB database:


const MongoClient = require('mongodb').MongoClient;
const uri = 'mongodb://localhost:27017/articles';

MongoClient.connect(uri, (err, client) => {
  if (err) {
    console.error(err);
    process.exit(1);
  }

  const db = client.db('articles');
});

8. Create a model for your articles. Create a model for your articles in MongoDB. This will define the structure of your articles data.


const Article = db.model('Article', {
  title: String,
  content: String,
  publishedAt: Date
});

9. Create routes for your API. Create routes for your API to allow users to create, read, update, and delete articles.


app.get('/articles', (req, res) => {
  const articles = Article.find();
  res.json(articles);
});

app.post('/articles', (req, res) => {
  const article = new Article({
    title: req.body.title,
    content: req.body.content,
    publishedAt: new Date()
  });

  article.save((err, article) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.json(article);
    }
  });
});

app.put('/articles/:id', (req, res) => {
  const article = Article.findById(req.params.id);

  if (!article) {
    res.status(404).send('Article not found');
    return;
  }

  article.title = req.body.title;
  article.content = req.body.content;
  article.publishedAt = new Date();

  article.save((err, article) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.json(article);
    }
  });
});

app.delete('/articles/:id', (req, res) => {
  const article = Article.findById(req.params.id);

  if (!article) {
    res.status(404).send('Article not found');
    return;
  }

  article.remove((err) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.sendStatus(200);
    }
  });
});

10. Test your API. You can test your API using a tool like Postman: https://www.postman.com/.

Copyright 2024. All rights are reserved