[Express] Level 3:massaging User Data

Source: Internet
Author: User

Flexible Routes

Our current route is only works if the city Name argument matches exactly the properties in the cities object. This is a problem. We need A-on-the-make and We code more flexible.

Inside our route, call the parseCityName() function passing in the name parameter. Assign The return value to the new variable called cityName .

var cityname = parsecityname (request.params.name); function Parsecityname (name) {  var parsedname = name[0].touppercase () + name.slice (1). toLowerCase () ;   return Parsedname;}

Now, using the city name returned from parseCityName() the function, lookup the corresponding description using the cities object a nd store it in the correct variable that would make the rest of the function work as intended.

  var cityname = parsecityname (request.params.name);   var cityinfo = Cities[cityname];

varExpress = require (' Express ');varApp =Express ();varCities = {  ' Lotopia ': ' Rough and Mountainous ',  ' Caspiana ': ' Sky-top Island ',  ' Indigo ': ' Vibrant and thriving ',  ' Paradise ': ' Lush, Green Plantation ',  ' Flotilla ': ' bustling urban oasis '};app.get ('/cities/:name ',function(Request, response) {varCityName =Parsecityname (request.params.name); varCityinfo =Cities[cityname]; if(cityinfo) {Response.json (cityinfo); } Else{Response.Status (404). JSON (' City not found '); }});functionParsecityname (name) {varParsedname = name[0].touppercase () + name.slice (1). toLowerCase (); returnParsedname;} App.listen (3000);

Dynamic Routes I

Which Express function maps placeholders to callback functions, and are commonly used for running pre-conditions on Dynamic Routes?

Answer:

App.param ();

Dynamic Routes II

Whenever we name parameter we want to parse it a specific. Let's existing code so, all routes with a name parameter get the same special handling.

Call to app.param() intercept requests this contain an argument called ‘name‘ . Remember app.param() takes a callback function as its second argument, which uses the same signature as a middleware.

var express = require (' Express '); var app = Express (); App.param (function(request, response, next) {});

Inside app.param() The callback function, call the parseCityName() function with the submitted name parameter. Set The return value to a new property in the request object called cityName .

function (Request, response, next) {    = parsecityname (request.params.name);});

Finally, call a function is moves processing to the next function in the stack.

function (Request, response, next) {  = parsecityname (request.params.name);  Next ();});

varExpress = require (' Express ');varApp =Express ();varCities = {  ' 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); Next ();}); App.get ('/cities/:name ',function(Request, response) {varCityinfo =Cities[request.cityname]; if(cityinfo) {Response.json (cityinfo); } Else{Response.Status (404). JSON ("City not Found"); }});functionParsecityname (name) {varParsedname = name[0].touppercase () + name.slice (1). toLowerCase (); returnParsedname;} App.listen (3000);

Dynamic Routes III

The following code has a Dynamic Route, that takes a, as an argument and returns, the city, created in. The problem and our current implementation are the IT breaks when invalid data are sent on client requests. Let ' s add some basic validation.

Call a function This intercepts Dynamic Routes with the ‘year‘ param.

function (Request, response, next) {});

Inside of this function, use the isYearFormat() function to check whether the year parameter are in a valid format. If So, then move processing to the next function in the stack.

  if (Isyearformat (request.params.year)) {    next ();  }

If year The parameter is not in a valid format, then respond with a , HTTP status code and a JSON message c12/>.

function (Request, response, next) {  if(Isyearformat (request.params.year)) {    next ();  } Else {      response.status. JSON (' Invalid Format for Year ');  }});

varExpress = require (' Express ');varApp =Express (); App.param (' Year ',function(Request, response, next) {if(Isyearformat (request.params.year)) {next (); }Else{Response.Status (. JSON (' Invalid Format for year '); }});varCitiesyear = {  : ' Lotopia ',  5100: ' Caspiana ',  5105: ' Indigo ',  6000: ' Paradise ',  7000: ' Flotilla '};functionIsyearformat (value) {varRegExp = RegExp (/^d{4}$/); returnregexp.test (value);} App.get ('/cities/year/:year ',function(Request, response) {varYear =request.params.year; varCity =Citiesyear[year]; if(!City ) {Response.Status (404). JSON ("No City found for given year"); } Else{Response.json (' In ' + year + ', ' + City + ' is created.); }}); App.listen (3000);

Dynamic Routes IV

With the proper validations on place for the following code, what would the output is for a GET request to /cities/year/500 ?

Answer:

[Express] level 3:massaging User Data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.