[Express] Level 4: Body-parser -- Delete
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的更多相关文章
- [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 ...
- [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 ...
- [Express] Level 4: Body-parser -- Post
Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express applic ...
- [Express] Level 3: Massaging User Data
Flexible Routes Our current route only works when the city name argument matches exactly the propert ...
- [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 ...
- [Express] Level 2: Middleware -- 2
Logging Middleware Help finish the following middleware code in the logger.js file: On the response ...
- [Express] Level 2: Middleware -- 1
Mounting Middleware Given an application instance is set to the app variable, which of the following ...
- [Express] Level 1: First Step
Installing Express Let's start building our new Express application by installing Express. Type the ...
- 基于express框架的应用程序骨架生成器介绍
作者:zhanhailiang 日期:2014-11-09 本文将介绍怎样使用express-generator工具高速生成基于express框架的应用程序骨架: 1. 安装express-gener ...
随机推荐
- python中隐式的内存共享
在python中,基本上使用的是引用,那么就会造成一个隐式的内存共享,特别是在容器对象中,例如list,dictionary 对于不可变对象,是不会造成隐式的内存共享情况,如下所示: >> ...
- python字典概述
字典 1. 概述 字典是一个无序的数据集合,序列类型用有序的数字键做索引将数据以数组的形式存储. 在字典中能获得的有序集合只能是键的集合或者是值得集合,方法keys()或者value()返回一个 ...
- 关系数据库&&NoSQL数据库
在过去,我们只需要学习和使用一种数据库技术,就能做几乎所有的数据库应用开发.因为成熟稳定的关系数据库产品并不是很多,而供你选择的免费版本就更加少了,所以互联网领域基本上都选择了免费的MySQL数据库. ...
- UML的类图关系分为: 关联、聚合/组合、依赖、泛化(继承)
UML的类图关系分为: 关联.聚合/组合.依赖.泛化(继承).而其中关联又分为双向关联.单向关联.自身关联:下面就让我们一起来看看这些关系究竟是什么,以及它们的区别在哪里. 1.关联 双向关联:C1- ...
- java、android 对比两个目录或文件是否是同一个目录或文件的方法
由于软链接及android的外部卡mount方式存在,导致一个文件夹可能同时有两个路径,如: /mnt/sdcard1 /storage/ext_sdcard ,如果通过某种方式(如moun ...
- cocos2dx android版本移植时的Error format not a string literal and no format arguments解决方案
原文地址 : http://www.cnblogs.com/hhuang2012/p/3336911.html cocos2dx android版本移植时的Error format not a str ...
- c# spring aop的简单例子
刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...
- unigui TUniTreeView demo
unit untTree; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- installshield Basic 工程每次安装完提示重启电脑
将Sequence中的ScheduleReboot Action的Condition清空即可.
- CloudStack4.2 二级镜像存储测试
//添加二级存储{ "addimagestoreresponse": { "imagestore": { "id": "2dda4 ...