RESTful API

RESTful API

REST stands for Representational State Transfer and is an architecture for building Web APIs. The HTTP(s) verbs it uses are
  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
Here is a complete example of implementing a RESTful API for a database that stores some articles:
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");

const app = express();

app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));

const wikiSchema = {
   title: String,
   content: String
};


const Article = mongoose.model("article", wikiSchema);

function sendExistingArticles(res) {
   Article.find({}, function (err, articles) {
       if (!err) {
           res.send(articles);
       }
       else {
           res.send(err);
       }
   });
}


mongoose.connect("mongodb://localhost:27017/wikiDB", { useNewUrlParser: true, useUnifiedTopology: true });

//Requests targeting a specific article //
app.route("/articles/:articleTitle")
   .get(function (req, res) {
       let requestedTitle = req.params.articleTitle;
       Article.findOne({ title: requestedTitle }, function (err, foundDocument) {
           if (!err) {
               if (foundDocument) {
                   res.send(foundDocument);
               }
               else {
                   res.send("Couldn't find article on " + requestedTitle);
               }
           }
           else {
               res.send(err);
           }
       });
   })
   .put(function (req, res) {
       let requestedTitle = req.params.articleTitle;
       Article.update({ title: requestedTitle },
           {
               title: req.body.title,
               content: req.body.content
           }
           , { overwrite: true },
           function (err, results) {
               if (err) {
                   res.send(err);
               }
               else {
                   res.send("Successfully updated article on " + requestedTitle);
               }

           });
   })
   .patch(function (req, res) {
       let requestedTitle = req.params.articleTitle;
       Article.update({ title: requestedTitle },
           { $set: req.body },
           function (err) { 
               if(!err) {
                   res.send("Successfully updated article on "+requestedTitle);
               }
               else {
                   res.send(err);
               }

           });
   })
   .delete(function(req,res) { 
       let requestedTitle = req.params.articleTitle;
       Article.findOneAndRemove({title: requestedTitle},function(err,foundDocument){ 
           if(!err){
               res.send("Removed document with title "+foundDocument.title);
           }
           else {
               res.send(err);
           }

       });
   });



//Requests targeting all articles //
app.route("/articles")
   .get(function (req, res) {
       sendExistingArticles(res);
   })
   .post(function (req, res) {
       const article = new Article({
           title: req.body.title,
           content: req.body.content
       });
       article.save(function (err) {
           if (!err) {
               res.send("Saved new article with title " + req.body.title);
           }
           else {
               res.send(err);
           }
       })
   })
   .delete(function (req, res) {
       Article.deleteMany(function (err) {
           if (!err) {
               res.send("Successfully deleted all the articles");
           }
           else {
               res.send(err);
           }

       });
   });



let port = 3000;
app.listen(port, function () {
   console.log("Server started on " + port);
});

References

The Complete 2020 Web Development Bootcamp Udemy

Comments

Popular posts from this blog

QTreeView and QTableView dynamic changes

C++ strings and string_view