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