Section 30 – Lesson 388 – Complete 2020 Web Development Bootcamp

Lesson 388 – Chained Route Handlers Using Express

Express allows you to create chainable route handlers using the app.route() method.

This is the current app.js file code before refactoring it to use the app.route() method –

const express = require("express");
const bodyParser = require("body-parser");

const mongoose = require("mongoose");
const ejs = require("ejs");

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, useUnifiedTopology: true});

const articleSchema = ({
  title: String,
  content: String
});

const Article = mongoose.model("Article", articleSchema);


app.get("/articles", function (req, res) {
  Article.find(function(err, foundArticles){
  	if (!err) {
  		res.send(foundArticles);
  	} else {
  		res.send(err);
  	}
  	
  });
});

app.post("/articles", function(req, res){
	

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

app.delete("/articles", function(req, res) {
	Article.deleteMany(function(err){
		if (!err) {
			res.send("Successfully deleted all articles.");
		} else {
			res.send(err);
		}
	});
});

app.listen(3000, function() {
  console.log("Server has started successfully.");
});

This is the refactored app.js code using the app.route() method –

const express = require("express");
const bodyParser = require("body-parser");

const mongoose = require("mongoose");
const ejs = require("ejs");

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, useUnifiedTopology: true});

const articleSchema = ({
  title: String,
  content: String
});

const Article = mongoose.model("Article", articleSchema);


app.route("/articles")

.get(function (req, res) {
  Article.find(function(err, foundArticles){
  	if (!err) {
  		res.send(foundArticles);
  	} else {
  		res.send(err);
  	}
  	
  });
})

.post(function(req, res){
	
	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) {
	Article.deleteMany(function(err){
		if (!err) {
			res.send("Successfully deleted all articles.");
		} else {
			res.send(err);
		}
	});
});

app.listen(3000, function() {
  console.log("Server has started successfully.");
});

So now instead of using a separate app.get() method, app.post() method and app.delete() method on the “/articles” route, you are using one app.route() method on the “/articles” route and inside of that app.route() method the .get(), .post() and .delete() methods have been chained.

Lesson 389 – GET a Specific Article

You now need to create a route to handle requests relating to a specific article.

You can do this using the app.route() method to chain the .get(), .post() and .delete() methods for a specific article to the same app.route() method.

The code to create an app.route() method to GET a specific article is –

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

.get(function (req, res) {

	Article.findOne({title: req.params.articleTitle}, function(err, foundArticle){
		if (foundArticle) {
			res.send(foundArticle);
		} else {
			res.send("No article matching that title was found.");
		}
	});

});

Lesson 390 – PUT a Specific Article

You now need to add the .put request relating to a specific article.

The code to create an app.route() method to PUT a specific article is –

.put(function(req, res) {
   Article.replaceOne(
	{title: req.params.articleTitle},
	{title: req.body.title, content: req.body.content},
	{overwrite: true},

	function(err) {
		if (!err) {
			res.send("Successfully updated article.");
		} 
		}
    );
	
});