[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 ...
随机推荐
- java中的类实现comparable接口 用于排序
import java.util.Arrays; public class SortApp { public static void main(String[] args) { Student[] s ...
- 我的日常工具——gdb篇
我的日常工具——gdb篇 03 Apr 2014 1.gdb的原理 熟悉linux的同学面试官会问你用过gdb么?那好用过,知道gdb是怎么工作的么?然后直接傻眼... gdb是怎么接管一个进程?并且 ...
- 黑马程序员——Block数据类型
Block数据类型,又被称为代码段.因为它可以封装一段代码.苹果官方建议多用block.因为在多线程控制.异步任务,集合遍历.集合排序.动画转场等方面用的很多. Block的特点: 1.Block 用 ...
- TopFreeTheme精选免费模板【20130827】
今天我们整理了一些关于WordPress, Joomla, Drupal, Magento, OpenCart的最新免费模板主题,绝大多数都是来自ThemeForest的响应式风格模板主题.题材多样化 ...
- NServiceBus-网关和多站点分布
多站点部署的企业的数量.净系统由于增加的挑战高可用性和用户要求更快的响应时间,服务器和数据访问更接近. RPC技术迅速陷入困境在这些环境中,使机器在同一个站点和远程站点看起来是一样的. 在这些情况下, ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- asp web api 怎么使用put和delete。
Method Overriding RESTful services allow the clients to act on the resources through methods such as ...
- CentOS下MySQL 5.7.9编译安装
MySQL 5.7 GA版本的发布,也就是说从现在开始5.7已经可以在生产环境中使用,有任何问题官方都将立刻修复. MySQL 5.7主要特性: 更好的性能:对于多核CPU.固态硬盘.锁有着更好的优化 ...
- DataTable的新建、查询、添加和修改
详细讲解了C#开发中数据类型DataTable的使用,包括新建DataTable,查询DataTable,在DataTable里添加记录,添加 列,修改列的属性以及修改某行某列的值的方法.同时说明了将 ...
- Struts – Multiple configuration files example
Many developers like to put all Struts related stuff (action, form) into a single Struts configurati ...