[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 ...
随机推荐
- VS2012打包部署Winform程序
打包前的准备工作: 新建一个打包部署项目,点OK,如果是第一次使用的话,会打开一个网页,按照提示的步骤来做, 点击上面的step2的网址,进入到另一个网页: 填写完右边的信息,点击“download ...
- Linux+mysql+apache+php+wordpress搭建个人空间
1. linux的安装 现在Linux的品种巨多,这个你可以选择一个你喜欢的linux系统,如果是新手并不建议你使用freebsd,gentoo等,建议你可以安装ubuntu,如果要安装u ...
- matlab 画锥体
>> plot3(x,y,z); >> [x,y,z]=cylinder([ ],)
- Determining Equality of Objects
[Determining Equality of Objects] If you need to determine whether one object is the same as another ...
- centos下apache安装后无法访问
2013.11.28遇到的问题: -------------------------------------- 一.centos下apache安装后无法访问 得查一下防火墙的问题 iptables添加 ...
- iOS版本检测与版本升级
14年苹果官方要求所有的APP不能出现 “当前版本”字样,是因为从iOS8系统开始,你可以在设置里面设置在WiFi情况下,自动更新安装的APP.此功能大大方便了用户,但是一些用户没有开 启此项功能,因 ...
- 给js function的参数设置默认值
C# 中 function func(a,b=1){//具体方法} js 中 function func(a,b){ a= arguments[0] || 10; b= arguments[1] || ...
- Error opening trace file: No such file or directory (2)
想看sharepreference保存的键值对的数据,用到单元测,运行函数总是报这种错,且看不到file explorer-->data>对应工程包的xml文件:
- C:数组
数组.排序 关于排序 :参考 关于数组: 参考 求a[i][j]行与列的和然后求平均值 参考 二维数组使用指针的表示方法 参考 字符串数组:char name [5][20] ={ {} , {} ...
- <meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/>
<meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/> 制定ie调用哪 ...