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. 数往知来 ASP.NET 表单的提交_url传值_重定向 <十八>

    一.表单提交时注意 如果是以get方式提交数据的时候,在接收时是用QueryString方式进行接收 如果是以post请求数据时在接收时是用Form进行接收 为什么么要这样做呢?我们用Request[ ...

  2. 大数据时代的数据存储,非关系型数据库MongoDB

    在过去的很长一段时间中,关系型数据库(Relational Database Management System)一直是最主流的数据库解决方案,他运用真实世界中事物与关系来解释数据库中抽象的数据架构. ...

  3. ps制作哈7海报字体

    模仿也需要较强的功底和分析思路.如下面的教程,作者模仿的是电影海报字.文字构造虽不复杂,不过思路不对的话就容易走弯路.最终效果 1.先来分析文字的构造,大致由两部分组成,一部分是浮雕字,另一部分是质感 ...

  4. 30+学习Web设计和开发的优质新鲜资源

    今天我们整理了一些最新的Web设计和开发的资源,这些资源都来自国外的流行站点,不过大家应该不会陌生,放在这里供大家收藏,在需要的时候方便翻阅和学习! 原文地址:http://www.goodfav.c ...

  5. 精美&创意的WordPress新发主题集合

    今天我整理了一些精美&创意的WordPress新发主题,它们基本上都融合了最新的设计理念,从简约到响应式,应有尽有. Engo – Smart & Minimal WordPress ...

  6. leetcode–jump game II

    1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...

  7. Lync激活用户遇到ConstraintViolationNoLeadingOrTrailingWhitespace错误

    启用用户的时候出现错误ConstraintViolationNoLeadingOrTrailingWhitespace,如下图 解决方案:域控中,该用户的名字最后多出了个空格,批量生成域用户的脚本问题 ...

  8. css 样式大全

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...

  9. koa 笔记 运行错误

    按照 演示的代码 直接运行会出错,大家需要调整方式. http://koajs.cn/ 要安装以下 $ npm install -g n$ n 0.11.12$ node --harmony my-k ...

  10. ipmotool

    ipmitool 命令收集 ipmitool 命令收集 from:http://blog.chinaunix.net/u2/70049/showart_1850139.html IPMI远程管理实验 ...