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 response.

app.get('/cities', function(request , response){

});

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

app.get('/cities', function(request , response){
if(request.query.search){ }
});

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

app.get('/cities', function(request , response){
var keyword = request.query.search;
if(keyword){
response.json(citySearch(keyword));
}
});
var express = require('express');
var app = express(); var cities = ['Caspiana', 'Indigo', 'Paradise']; app.get('/cities', function(request , response){
var keyword = request.query.search;
if(keyword){
response.json(citySearch(keyword));
}
}); function citySearch (keyword) {
var regexp = RegExp(keyword, 'i');
var result = cities.filter(function (city) {
return city.match(regexp);
}); return result;
} app.listen(3000);

Dynamic Route Variables

Consider the following Dynamic Route:

app.get('/cities/:name', function (request, response) {
// ...
})

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

Answer:

requst.params.name

City Information

Now lets look up some information about the city.

Inside of our dynamic route, grab the name submitted by the user, lookup the city information on the cities object and assign it to the cityInfovariable.

var cities = {
'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) {
var cityInfo,
name;
name = request.params.name;
cityInfo = cities[name];
});

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

app.get('/cities/:name', function (request, response) {
var cityInfo,
name;
name = request.params.name;
cityInfo = cities[name]; if(cityInfo){
response.json(cityInfo);
}
});

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

app.get('/cities/:name', function (request, response) {
var cityInfo,
name;
name = request.params.name;
cityInfo = cities[name]; if(cityInfo){
response.json(cityInfo);
}else{
response.status(404).json("City not found");
}
});
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.get('/cities/:name', function (request, response) {
var cityInfo,
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的更多相关文章

  1. [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 f ...

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

    Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express applic ...

  3. [Express] Level 2: Middleware -- 1

    Mounting Middleware Given an application instance is set to the app variable, which of the following ...

  4. [Express] Level 1: First Step

    Installing Express Let's start building our new Express application by installing Express. Type the ...

  5. Zabbix监控Low level discovery实时监控网站URL状态

    今天我们来聊一聊Low level discovery这个功能,我们为什么要用到loe level discovery这个功能呢? 很多时候,在使用zabbix监控一些东西,需要对类似于Itens进行 ...

  6. [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 ...

  7. [Express] Level 4: Body-parser -- Delete

    Response Body What would the response body be set to on a DELETE request to /cities/DoesNotExist ? H ...

  8. [Express] Level 3: Massaging User Data

    Flexible Routes Our current route only works when the city name argument matches exactly the propert ...

  9. [Express] Level 2: Middleware -- 2

    Logging Middleware Help finish the following middleware code in the logger.js file: On the response  ...

随机推荐

  1. 面试java简答题

    1.  sleep() 和 wait() 有什么区别? 答:1.这两个方法来自不同的类分别是Thread和Object      2.最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他 ...

  2. WCF_Config頁面常用配置

    右键点击App.config文件,选中Edit WCF Configuration进行编辑,我们添加2个baseAddress,一个是基于HTTP协议的:一个是基于TCP协议的.同时添加2个bindi ...

  3. Java自带webservice

    http://blog.sina.com.cn/s/blog_61d8d96401013tmp.html 1.首先创建一个Java项目,作为Web services Endpoint. 2.创建一个H ...

  4. 流式计算之Storm简介

    Storm是一个分布式的.容错的实时计算系统,遵循Eclipse Public License 1.0,Storm可以方便地在一个计算机集群中编写与扩展复杂的实时计算,Storm之于实时处理,就好比H ...

  5. 无线网WEP的安全测试及防范

    650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/att ...

  6. cocos2d-x使用python创建vs模板

    cocos2d-x 2.2推荐使用create_project.py创建工程,所有的平台都可以通过这个python文件创建工程.这个文件位置在源码cocos2d-x-2.2.2\tools\proje ...

  7. 应用TcpListener实现的socket服务器端

    前言 项目中要实现一个简单的socket服务器端,采用了TcpListener这个类.除了基本的功能之外,有几处需要注意的点. 要能同时接收多个客户端的连接,当然,不需要几千个那么多. 要能探测到客户 ...

  8. python 递归遍历文件夹

    #!/usr/bin/python import os.path def readXmls(folder): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 for ...

  9. HDOJ_1010 Tempter of the Bone

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 奇偶剪枝:可以把map看成这样: 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 ...

  10. 成功获取并更改中兴F660光猫的超级用户密码解除四台限制

    上次雷雨后更换的中兴的F660光猫还是很不错的,很稳定,不过超级密码确实记不住,找了些资料,今天成功的更改了密码,简要的写出过程以备下次参考: 第一步:获取超级密码(已知用户名telecomadmin ...