[Express] Level 3: Reading from the URL
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的更多相关文章
- [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 ...
- [Express] Level 4: Body-parser -- Post
Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express applic ...
- [Express] Level 2: Middleware -- 1
Mounting Middleware Given an application instance is set to the app variable, which of the following ...
- [Express] Level 1: First Step
Installing Express Let's start building our new Express application by installing Express. Type the ...
- Zabbix监控Low level discovery实时监控网站URL状态
今天我们来聊一聊Low level discovery这个功能,我们为什么要用到loe level discovery这个功能呢? 很多时候,在使用zabbix监控一些东西,需要对类似于Itens进行 ...
- [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 ...
- [Express] Level 4: Body-parser -- Delete
Response Body What would the response body be set to on a DELETE request to /cities/DoesNotExist ? H ...
- [Express] Level 3: Massaging User Data
Flexible Routes Our current route only works when the city name argument matches exactly the propert ...
- [Express] Level 2: Middleware -- 2
Logging Middleware Help finish the following middleware code in the logger.js file: On the response ...
随机推荐
- C语言的 (强制类型转换) 以及 '字符字面值'
C语言的显式/隐式类型转换,都有一个中间变量的存在,原数据的类型.内容都不变. 以下代码,都用GCC编译. #include<stdio.h> int main() { char c = ...
- 你可能不知道的30个Python语言的特点技巧
1 介绍 从我开始学习Python时我就决定维护一个经常使用的“窍门”列表.不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中.在StackOverflow.在开源码软件中,等等), ...
- 数往知来 HTML<十一>
HTML_CSS <!--一.表单 <form></form> 表单就是用来进行数据提交的标签 表单就是一对<form></form>标 ...
- Start SparkR in RStudio
Sys.setenv(SPARK_HOME="/usr/spark") .libPaths(c(file.path(Sys.getenv("SPARK_HOME" ...
- 很棒的Sketch动画教程
就像别人可以用PPT做动画,而你只会用它做演示,别人可以拿ps做gif,你却只会用它p照片.软件就是这样,我们使用大多数的软件也就是了解的程度,很难算得上精通.(后面补充了小教程,想看干货的直接看后面 ...
- 如何让Java和C++接口互相调用:JNI使用指南
如何让Java和C++接口互相调用:JNI使用指南 转自:http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos ...
- C++11静态assert
[C++11静态assert] C++11新的关键字static_assert可以解决模板中的状态检察. 声明采取以下的形式: 这里有一些如何使用static_assert的例子: 当常数表达式值为f ...
- labview在线帮助网址
http://zone.ni.com/reference/zhs-XX/help/371361L-0118/ labview网络讲坛 网址 http://v.eepw.com.cn/video/com ...
- 对比AMD 890、AMD 880、 AMD 790、AMD 785、 AMD 780、AMD 7
770无集显.中低端独显主流. 780G带集显.现在可以无视. 785G现在是带集显的主流. 790GX高端带集显. 790FX专高端,无集显. 790X带集显.基本无视. 870 大板,无集显 88 ...
- Python类的探讨
我们下面的探讨基于Python3,我实际测试使用的是Python3.2,Python3与Python2在类函数的类型上做了改变 1,类定义语法 Python类定义以关键字class开头,一个类定义例 ...