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. ArcGIS API Reference & Flex API samples学习进度备忘

    书签:跳过:另外跳过的内容有待跟进 __________________学习资源: 1.http://help.arcgis.com/en/webapi/flex/apiref/index.html ...

  2. 配置Texmaker中文支持

    在Ubuntu 12.04 LTS下安装Texmaker后,如需要支持中文环境,需要安装CJK包. 终端(Ctrl+Alt+T(Terminal))下输入命令切换到超级管理员: sudo -i 安装包 ...

  3. [转]一些实用的图表Chart制作工具

    最近工作过程中需要用到前端一些JS框架,看到一篇博文就转过来备份使用,后续会再完善一些材料.   Flot   Flot一个纯javascript绘画库,基于jQuery开发.它能够在客户端根据任何数 ...

  4. debian7下部署nginx服务器

    笔者是在vmware中的Debian7下部署nginx服务器,采用离线部署方式.过程如下: 1.准备好需要的离线安装包 nginx-1.6.2.tar.gz,pcre-8.34.tar.gz,open ...

  5. js运动 多物体运动含Json 但是里面数值不一样

    <!doctype html> <html> <head> <meta charset = "utf-8"> <title&g ...

  6. LINQ标准查询操作符(四) —AsEnumerable,Cast,OfType,ToArray,ToDictionary,ToList,ToLookup,First,Last,ElementAt

    十.转换操作符 转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以“As”开头的转换方法可更改源集合的静态类型但不枚举(延迟加载)此源集合.名称以“To”开头的方法可枚举(即时加载)源集合并 ...

  7. Struts2文件下载

    1). Struts2 中使用 type="stream" 的 result 进行下载 2). 可以为 stream 的 result 设定如下参数 contentType: 结果 ...

  8. HDU 2042 不容易系列之二 [补6.24] 分类: ACM 2015-06-26 20:40 9人阅读 评论(0) 收藏

    不容易系列之二 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  9. stm32F4各个库文件的作用分析

    system_stm32f4xx.c:This file contains the system clock configuration for STM32F4xx devices. /** **** ...

  10. 创建一个EMS 扩展包

    EMS Package 向导: File > New > Other > Delphi projects > EMS > EMS Package Empty packag ...