Boilerplate: RESTful

 

//jshint esversion:6

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}));

app.use(express.static("public"));

 

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

const articleSchema = {

  title: String,

  content: String

};

const Article = mongoose.model("Article", articleSchema); // remember capitalized, singular form

 

////////Requests Targetting all Articles////////

 

app.route("/articles")

.get(function(req, res){

  Article.find(function(err, foundArticles){  // <ModelName>.find find all articles

    if (!err) {

      res.send(foundArticles);

    } else {

      res.send(err);

    }

  });

})

 

.post(function(req, res){                  //add a new entry

  const newArticle = new Article({

    title: req.body.title,

    content: req.body.content

  });

  newArticle.save(function(err){

    if (!err){

      res.send("Successfully added a new article.");

    } else {

      res.send(err);

    }

  });

})

 

.delete(function(req, res){         // delete all the entries

  Article.deleteMany(function(err){

    if (!err){

      res.send("Successfully deleted all articles.");

    } else {

      res.send(err);

    }

  });

});

 

/////////Requests Targetting A Specific Article///////

 

app.route("/articles/:articleTitle")

.get(function(req, res){

  Article.findOne({title: req.params.articleTitle}, function(err, foundArticle){ // find one document

    if (foundArticle) {

      res.send(foundArticle);

    } else {

      res.send("No articles matching that title was found.");

    }

  });

})

 

.put(function(req, res){  //update an entry

  Article.update(       // model name

    {title: req.params.articleTitle},   // condition

    {title: req.body.title, content: req.body.content}, //updates exactly as what is put here. If a field is missing, it will update the database as Null or wipe out that field

    {overwrite: true},

    function(err){

      if(!err){

        res.send("Successfully updated the selected article.");

      }

    }

  );

})

 

.patch(function(req, res){  //update some fields of an entry

  Article.update(

    {title: req.params.articleTitle}, // conditions

    {$set: req.body}, // update a certain field

    function(err){

      if(!err){

        res.send("Successfully updated article.");

      } else {

        res.send(err);

      }

    }

  );

})

 

.delete(function(req, res){

  Article.deleteOne(  //delete one entry

{title: req.params.articleTitle},  //condition

    function(err){

      if (!err){

        res.send("Successfully deleted the corresponding article.");

      } else {

        res.send(err);

      }

    }

  );

});

 

app.listen(3000, function() {

  console.log("Server started on port 3000");

});

 

0 Comments