[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 ...
随机推荐
- BITED数学建模七日谈之四:数学模型分类浅谈
本文进入到数学建模七日谈第四天:数学模型分类浅谈 大家常常问道,数学模型到底有哪些,分别该怎么学习,这样能让我们的学习有的放矢,而不至于没了方向.我想告诉大家,现实生活中的问题有哪些类,数学模型就有哪 ...
- Netmask v. Address Prefix Length
Netmask Address Prefix Length Hosts / Class C's / Class B's / Class A's (Class C) / / , / , / , / , ...
- 【boost】MFC dll中使用boost thread的问题
项目需要,在MFC dll中使用了boost thread(<boost/thread.hpp>),LoadLibraryEx的时候出现断言错误,去掉thread库引用后断言消失. 百度g ...
- Array.prototype.slice.call
Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 ,::'age'}; Array.prototype.slice.call(arr); ...
- c++builder 重载WindowProc、WndProc 截获消息
c++builder 重载WindowProc.WndProc 截获消息 方法一WindowProc void __fastcall myWindowProc(Messages::TMessage ...
- vs常用插件之javsscript插件
1.JSEnhancements 折叠JS和CSS代码 http://visualstudiogallery.msdn.microsoft.com/0696ad60-1c68-4b2a-9646-4b ...
- axis1调用方式
axis http://10.15.22.28/itfmgr/services/ITaxManagement?wsdl package com.isoftstone.core.service.impl ...
- python知识点 07-11
python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 python的 nonlocal关键字用来在函数或其他作用域中使用 ...
- hdu 1084 What Is Your Grade?
http://acm.hdu.edu.cn/showproblem.php?pid=1084 What Is Your Grade? Time Limit: 2000/1000 MS (Java/Ot ...
- POJ1469COURSES(二分图匹配)
裸的二分图匹配 题目poj.org/problem?id=1469 不解释: