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. linux 为开机菜单加密码·

    首先是在/boot/grub/menu.lst 里面添加密码的,但是需要是加密过后的,否则人家直接跑到你的menu.lst里面查看密码不就行了.... 于是,可以使用grub提供的md5加密功能: # ...

  2. linux modprobe.conf怎么不见了—-CentOS 6

    用习惯了CentOS或RHEL的用户或许很熟悉/etc/modprobe.conf文件,系统声卡.网卡.SCSI卡的驱动类型都在这里定义,同样部分动态加载的模块的参数也可以在这个文件中定义,但是在Ce ...

  3. Epic - Seed Number

    Find the seed of a number. Eg : 1716 = 143*1*4*3 =1716 so 143 is the seed of 1716. find all possible ...

  4. SpringMVC处理Date类型的成员变量方法

    原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...

  5. HTML5每日一练之progress标签的应用

    progress标签:从名字上来看,估计大家也能猜到这个标签是什么标签了,没错,他是一个进度条.在HTML5中我们终于可以不用模拟了. <progress id="W3Cfuns_pr ...

  6. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  7. AcceptEx与WSAEventSelect和Accept

    (转自论坛的一个帖子http://bbs.csdn.net/topics/280032853) AcceptEx主要用于向完成端口 投递一个或多个的连接请求..当有连接时进来,这里分两种情况: 1.A ...

  8. C#取得当前目录 转载

    /获取包含清单的已加载文件的路径或 UNC 位置.         public static string sApplicationPath = Assembly.GetExecutingAssem ...

  9. PowerDesigner反向生成Mysql数据原型

    PowerDesigner反向生成Mysql数据原型 注意事项: (1)JVM 要32位的. (2)需配置JAVA_HOME环境变量指向所需JVM. (3)需配置CLASSPATH环境变量执行 MyS ...

  10. 转载:Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式

    Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式 出自:http://www.cnblogs.com/top5/archive/2012/08/04/2623464.html 关 ...