Response Body

What would the response body be set to on a DELETE request to /cities/DoesNotExist ? Here's the link to the sendStatus function source code if you need to take a look.

Answer: 404

Delete Route

Create a Dynamic Route for deleting cities and handle for cities that are not in our list.

Create a DELETE route that takes the city name as its first argument, followed by a callback that takes a request and response objects as arguments.

app.delete('/cities/:name', function(request, response){

});

Use the built-in JavaScript operator delete (see MDN docs) to remove the property for the city passed as an argument. Don't forget to use the attribute set in app.param() to look the city up.

app.param('name', function (request, response, next) {
  request.cityName = parseCityName(request.params.name);
});

app.delete('/cities/:name', function(request, response){
    delete cities[request.cityName];
});

Use sendStatus() to respond back with a status code of 200.

app.delete('/cities/:name', function(request, response){
    delete cities[request.cityName];
  response.sendStatus(200);
});

Add an if block that checks whether the cityName provided fromapp.param() has a valid entry before attempting to delete it from thecities object. If a valid city is not found, then respond with a 404 HTTP status code using the sendStatus() function.

app.delete('/cities/:name', function(request, response){
  if(!cities[request.cityName]){
      response.sendStatus(404);
  }else{
      delete cities[request.cityName];
    response.sendStatus(200);
  }
});
var express = require('express');
var app = express();

var cities = {
  'Lotopia': 'Rough and mountainous',
  'Caspiana': 'Sky-top island',
  'Indigo': 'Vibrant and thriving',
  'Paradise': 'Lush, green plantation',
  'Flotilla': 'Bustling urban oasis'
};

app.param('name', function (request, response, next) {
  request.cityName = parseCityName(request.params.name);
});

app.delete('/cities/:name', function(request, response){
  if(!cities[request.cityName]){
      response.sendStatus(404);
  }else{
      delete cities[request.cityName];
    response.sendStatus(200);
  }
});

app.listen(3000);

function parseCityName(name) {
  var parsedName = name[0].toUpperCase() + name.slice(1).toLowerCase();
  return parsedName;
}

[Express] Level 4: Body-parser -- Delete的更多相关文章

  1. [Express] Level 5: Route file

    Using a Router Instance Let's refactor app.js to use a Router object. Create a new router object and ...

  2. [Express] Level 5: Route Instance -- refactor the code

    Route Instance Let's rewrite our cities routes using a Route Instance. Create a new Route Instance f ...

  3. [Express] Level 4: Body-parser -- Post

    Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express applic ...

  4. [Express] Level 3: Massaging User Data

    Flexible Routes Our current route only works when the city name argument matches exactly the propert ...

  5. [Express] Level 3: Reading from the URL

    City Search We want to create an endpoint that we can use to filter cities. Follow the tasks below t ...

  6. [Express] Level 2: Middleware -- 2

    Logging Middleware Help finish the following middleware code in the logger.js file: On the response  ...

  7. [Express] Level 2: Middleware -- 1

    Mounting Middleware Given an application instance is set to the app variable, which of the following ...

  8. [Express] Level 1: First Step

    Installing Express Let's start building our new Express application by installing Express. Type the ...

  9. 基于express框架的应用程序骨架生成器介绍

    作者:zhanhailiang 日期:2014-11-09 本文将介绍怎样使用express-generator工具高速生成基于express框架的应用程序骨架: 1. 安装express-gener ...

随机推荐

  1. 关于EventSource的精华

    他是keep-alive的连接,服务端持续向这个请求的Reponse发送数据,以"data: "+您的消息+"\n\n"的格式发送,浏览器端会收到您发送的消息. ...

  2. SignalR 远程访问并跨域

    http://stackoverflow.com/questions/16875228/how-do-i-get-a-signalr-hub-connection-to-work-cross-doma ...

  3. 中国B2B行业将进入后平台时代

    中国的B2B正在进入后平台时代,我们用三个特征来诠释这个词:1.电商平台构建流程闭环服务,从电商平台向综合服务商转变:2.新流量批发策略的落地代表B2B找到了新入口:3.第三方服务的价值变现与第二波创 ...

  4. 控制反转容器& 依赖注入模式 ---读感。

    几个web框架 : sprint Avalon PicoContainerclass MovieLister MovieFinder finder = ServiceLocator.movieFind ...

  5. 网络&热恋NSURLConnection代理及GET¥POST请求

    1.NSURLConnection代理下载设置在本地的身骑着白马MP3 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...

  6. typeof操作符在javascript中运用时时页面上的操作数显示

    typeof可以告诉我们它的操作数是一个字符串(string).数值(number).函数(function).布尔值(boolean)或对象(object). 1.字符串(string) alert ...

  7. There is no getter for property named 'NULL' in ……

    往往细节上的错误事最要命的事情,当你看着代码,逻辑上没有问题,但是却又曝出一些莫名其妙不知所以的错,你百度了 说出来的原因又是乱七八糟的鸡肋!很无助,纠结了很久,浪费了很多宝贵的时间--看代码! &l ...

  8. 将Axure用于需求分析工具

    http://www.cnblogs.com/hnlong1/p/4113517.html?utm_source=tuicool 今年以来开始接触需求分析工作,uml是必用的建模语言. 一开始是使用最 ...

  9. July 23rd, Week 30th Saturday, 2016

    A day is a miniature of eternity. 一天是永恒的缩影. For a man, the eternity is his lifetime which is measure ...

  10. sql复制表、拷贝表、临时表

    --insert into pppcopy select * from ppp //从表ppp中获取数据,并将其插入到pppcopy中,只拷贝表的数据,不拷贝表的结构(前提:表pppcopy1存在) ...