// app.js

var express = require('express');
var app = express();

var bodyParser = require('body-parser');
var parseUrlencoded = bodyParser.urlencoded({extended: false});

var blocks = {
  fixed: 'fasten securely in position',
  movable: 'capable of being moved',
  rotating: 'move in a circle around its center'
}

// use app.param() to intercept request
// convert all the letters the param 'name' to lower case
app.param('name', (req, res, next) => {
  req.params.name = req.params.name.toLowerCase();
  next();
})

// route with query string
app.get('/blocks', (req, res) => {
  var blocksNames = Object.keys(blocks);
  if (req.query.limit > 0) {
    res.json(blocksNames.slice(0, req.query.limit));
  } else {
    res.json(blocksNames);
  }
})

// dynamic route with params
app.get('/blocks/:name', (req, res) => {

  // handle path not found case
  var desc = blocks[req.params.name];
  if (!desc) {
    res.status(404).json('cannot find ' + req.params.name);
  } else {
    res.status(200).json(desc);
  }
})

app.post('/blocks', parseUrlencoded, (req, res) => {
  var newBlock = req.body;

  // console.log('req body', req.body);
  blocks[newBlock.name] = newBlock.description;
  // console.log('blocks', blocks);

  res.status(201).json(newBlock.name);
})

app.delete('/blocks/:name', (req, res) => {
  delete blocks[req.params.name];
  
  // console.log('blocks', blocks);
  res.sendStatus(200);
})

app.listen(3000);

@@ -513,16 +513,37 @@
 ();%0A%7D)%0A%0A
+app.route('/blocks')%0A
 // route
@@ -565,27 +565,15 @@
 ing%0A
-app
+  
 .get(
-'/blocks', 
 (req
@@ -580,24 +580,26 @@
 , res) =%3E %7B%0A
+  
   var blocks
@@ -625,24 +625,26 @@
 (blocks);%0A  
+ 
+ 
 if (req.quer
@@ -654,24 +654,26 @@
 imit %3E 0) %7B%0A
+  
     res.json
@@ -713,16 +713,18 @@
 imit));%0A
+  
   %7D else
@@ -726,24 +726,26 @@
  else %7B%0A    
+  
 res.json(blo
@@ -755,20 +755,272 @@
 Names);%0A
+  
   %7D%0A
+  %7D)%0A  .post(parseUrlencoded, (req, res) =%3E %7B%0A    var newBlock = req.body;%0A%0A    // console.log('req body', req.body);%0A    blocks%5BnewBlock.name%5D = newBlock.description;%0A    // console.log('blocks', blocks);%0A%0A    res.status(201).json(newBlock.name);%0A  
 %7D)%0A%0A// d
@@ -1048,19 +1048,21 @@
 ams%0Aapp.
-get
+route
 ('/block
@@ -1061,34 +1061,41 @@
 ('/blocks/:name'
-, 
+)%0A  .get(
 (req, res) =%3E %7B%0A
@@ -1095,16 +1095,18 @@
 ) =%3E %7B%0A%0A
+  
   // han
@@ -1129,16 +1129,18 @@
 nd case%0A
+  
   var de
@@ -1169,16 +1169,18 @@
 .name%5D;%0A
+  
   if (!d
@@ -1182,24 +1182,26 @@
 f (!desc) %7B%0A
+  
     res.stat
@@ -1250,16 +1250,18 @@
 ame);%0A  
+ 
+ 
 %7D else %7B
@@ -1257,24 +1257,26 @@
    %7D else %7B%0A
+  
     res.stat
@@ -1301,291 +1301,27 @@
 ;%0A  
-%7D%0A%7D)%0A%0Aapp.post('/blocks', parseUrlencoded, (req, res) =%3E %7B%0A  var newBlock = req.body;%0A%0A  // console.log('req body', req.body);%0A  blocks%5BnewBlock.name%5D = newBlock.description;%0A  // console.log('blocks', blocks);%0A%0A  res.status(201).json(newBlock.name);%0A%7D)%0A%0Aapp.delete('/blocks/:name', 
+  %7D%0A  %7D)%0A  .delete(
 (req
@@ -1334,16 +1334,18 @@
  =%3E %7B%0A  
+ 
+ 
 delete b
@@ -1370,17 +1370,21 @@
 ame%5D;%0A  
-%0A
+  %0A  
   // con
@@ -1411,16 +1411,18 @@
 locks);%0A
+  
   res.se
@@ -1436,16 +1436,18 @@
 s(200);%0A
+  
 %7D)%0A%0Aapp.

use chain function to consolidate routes