博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Express] Level 4: Body-parser -- Delete
阅读量:6078 次
发布时间:2019-06-20

本文共 2234 字,大约阅读时间需要 7 分钟。

Response Body

What would the response body be set to on a DELETE request to /cities/DoesNotExist ? Here's  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 () 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;}

 

转载地址:http://xyxgx.baihongyu.com/

你可能感兴趣的文章
Linux下MEncoder的编译
查看>>
Xamarin使用ListView开启分组视图Cell数据展示bug处理
查看>>
Javascript中闭包(Closure)的探索(一)-基本概念
查看>>
spark高级排序彻底解秘
查看>>
ylbtech-LanguageSamples-PartialTypes(部分类型)
查看>>
福建省促进大数据发展:变分散式管理为统筹集中式管理
查看>>
开发环境、生产环境、测试环境的基本理解和区别
查看>>
tomcat多应用之间如何共享jar
查看>>
Flex前后台交互,service层调用后台服务的简单封装
查看>>
MySQL入门12-数据类型
查看>>
Windows Azure 保留已存在的虚拟网络外网IP(云服务)
查看>>
修改字符集
查看>>
HackTheGame 攻略 - 第四关
查看>>
js删除数组元素
查看>>
带空格文件名的处理(find xargs grep ..etc)
查看>>
华为Access、Hybrid和Trunk的区别和设置
查看>>
centos使用docker下安装mysql并配置、nginx
查看>>
关于HTML5的理解
查看>>
需要学的东西
查看>>
Internet Message Access Protocol --- IMAP协议
查看>>