[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;
}
时间: 2024-12-25 14:02:28

[Express] Level 4: Body-parser -- Delete的相关文章

[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 for the '/cities' URL path and assign it to thecitiesRoute variable. var citiesRoute = app.route('/cities'); Move the code from our previous app.get() 

[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 assign it to the router variable. var router = express.Router(); When we are done, our router will be mounted on the /cities path. With this in mind,

[Express] Level 4: Body-parser -- Post

Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express application. npm install body-parser Require the body-parser npm module and assign it to a variable calledbodyParser. var bodyParser = require('body-parser'

[Express] Level 2: Middleware -- 2

Logging Middleware Help finish the following middleware code in the logger.js file: On the response object, listen to the event that's emitted when the response has been handed off from Express to the underlying Operating System. response.on('finish'

[Express] Level 2: Middleware -- 1

Mounting Middleware Given an application instance is set to the app variable, which of the following function calls would you use to mount a middleware called logger ? Answer: app.use(logger); Default Middleware What is the only middleware that's shi

[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 to to create this new route. Create a new route for GET request to '/cities'. The second argument should be a callback function which takes request and

[Express] Level 3: Massaging User Data

Flexible Routes Our current route only works when the city name argument matches exactly the properties in the cities object. This is a problem. We need a way to make our code more flexible. Inside our route, call the parseCityName() function passing

express框架

前面的话 Express是一个简洁.灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮助开发者创建各种 Web 和移动设备应用.本文将详细介绍express框架 概述 官网对Express的描述,它是一个基于 Node.js 平台,快速.开放.极简的 web 开发框架.优点是易上手.高性能.扩展性强 1.易上手:nodejs最初就是为了开发高性能web服务器而被设计出来的,然而相对底层的API会让不少新手望而却步.express对web开发相关的模块进行了适度的封装,屏蔽

node、express框架

前面的话 Express是一个简洁.灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮助开发者创建各种 Web 和移动设备应用.本文将详细介绍express框架 概述 官网对Express的描述,它是一个基于 Node.js 平台,快速.开放.极简的 web 开发框架.优点是易上手.高性能.扩展性强 1.易上手:nodejs最初就是为了开发高性能web服务器而被设计出来的,然而相对底层的API会让不少新手望而却步.express对web开发相关的模块进行了适度的封装,屏蔽