[Express] Level 3:reading from the URL

Source: Internet
Author: User

City Search

We want to create a endpoint that we can use to filter cities. Follow the tasks below 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 response .

function (Request, Response) {    });

From inside of We route, create an if statement, checks whether a value was set to the query string parameter search .

function (Request, Response) {  if(request.query.search) {    }});

Inside if of the block, call citySearch() the function, passing in the user submitted parameter for search. Then return the result of the function as a JSON response.

function (Request, Response) {  var keyword = request.query.search;   if (keyword) {      response.json (citysearch (keyword));  });

varExpress = require (' Express ');varApp =Express ();varCities = [' Caspiana ', ' Indigo ', ' Paradise '];app.get ('/cities ',function(Request, response) {varKeyword =Request.query.search; if(keyword) {response.json (citysearch (keyword)); }});functioncitysearch (keyword) {varRegExp = regexp (keyword, ' i '); varresult = Cities.filter (function(city) {returnCity.match (regexp);  }); returnresult;} App.listen (3000);

Dynamic Route Variables

Consider the following Dynamic Route:

function (Request, Response) {  //  ...})

When requests come on for this route, what can we access the city name submitted by the user?

Answer:

Requst.params.name

City Information

Now lets look up some information on the city.

Inside of our dynamic route, grab the name submitted by the user, lookup the city information on the cities object and Assi GN it to the cityInfo variable.

var cities = {  ' lotopia ': ' Rough and Mountainous ',  ' caspiana ': ' Sky-top Island '  ,  ' Indigo ': ' Vibrant and thriving ',  ' Paradise ': ' Lush, Green Plantation ',  ' Flotilla ': ' bustling urban Oasis '};app.get (function  (request, Response) {   var Cityinfo,      name;   = request.params.name;   = cities[name];});

Check to see if cityInfo exists and if so, respond with the cityinfo in JSON format.

function (Request, Response) {  var  cityinfo,      name;   = request.params.name;   = Cities[name];     if (cityinfo) {      Response.json (cityinfo);  }});

If cityInfo does not exist, respond with a 404 the HTTP status code and a JSON message that says "City not found" .

function (Request, Response) {  var  cityinfo,      name;   = request.params.name;   = Cities[name];     if (cityinfo) {      Response.json (cityinfo);  } Else {      response.status (404). JSON ("City not Found");}  );

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) {varCityinfo, name; Name=Request.params.name; Cityinfo=Cities[name]; if(cityinfo) {Response.json (cityinfo); }Else{Response.Status (404). JSON ("City not Found"); }}); App.listen (3000);

[Express] Level 3:reading from the URL

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.