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. 坑爹的gltools编译错误解决

    搭边手游,看opengl superbible, 书中代码需要一个gltools库.作者自己提供的code google地址里gltools项目是空的,此一坑. 他的网站(www.starstones ...

  2. ansible官方文档翻译之变量

    Ansible变量 在使用ansible变量的时候,主要是因为各个系统的不同,从而需要使用不同的变量来进行设置,例如在设置一些配置文件的时候,有大部分内容是相同的,但是一部分内容是和主机的ip地址或者 ...

  3. SQL SERVER 2008 R2 SP3 发布

    今晚上刚发现,微软很低调啊 下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=44271 整合SP3的Express系列版本还没 ...

  4. 遵守GPL的开源软件能用于商用吗?

    遵守GPL的开源软件能用于商用吗? 比较经典的开源协议有 GPL,BSD 等等. GPL 软件可以用于商业用途,甚至说,RMS 撰写 GPL 协议的目的就是为了让自己的 GPL 软件 emacs 可以 ...

  5. 【Excel】Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046}:

    [Excel]Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-0000000000 ...

  6. 清空easyui datagrid

    $('#grid').datagrid("loadData",{total:0,rows:[]});

  7. 【转】Hive导入10G数据的测试

    原博文出自于: http://blog.fens.me/hadoop-hive-10g/ 感谢! Hive导入10G数据的测试 让Hadoop跑在云端系列文章,介绍了如何整合虚拟化和Hadoop,让H ...

  8. Java的位运算符—— 与(&)、非(~)、或(|)、异或(^)

    位运算符主要针对二进制,它包括了:“与”.“非”.“或”.“异或”.从表面上看似乎有点像逻辑运算符,但逻辑运算符是针对两个关系运算符来进行逻辑运算,而位运算符主要针对两个二进制数的位进行逻辑运算.下面 ...

  9. hdu 1044 Collect More Jewels(bfs+状态压缩)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  10. 使用logmnr方法找回被误删除Oracle的数据的脚本

    俗话说,常在河边走,哪有不湿鞋的.作为一个经常与数据库打交道的程序员,偶尔不小心误删除或误操作的数据也是在所难免的.如果是Oracle数据库,这里给您介绍一种从日志中找回数据的办法,下面这个地址是我以 ...