博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Express] Level 3: Reading from the URL
阅读量:6693 次
发布时间:2019-06-25

本文共 3215 字,大约阅读时间需要 10 分钟。

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);

 

转载地址:http://emcoo.baihongyu.com/

你可能感兴趣的文章
Telnet服务及协议
查看>>
SpringMVC深度探险
查看>>
关于vs2010巨慢(cpu占用高)的几种解决方式
查看>>
简单3步,轻松集成Testlink和MantisBT
查看>>
SQL语句教程(04) AND OR
查看>>
EBS 12.1.3 db 11.2.3 dg AND DG SWITCH OVER
查看>>
Oracle中的JOIN
查看>>
html中iframe控制父页面刷新
查看>>
每天一个linux命令(50):crontab命令
查看>>
linux命令7--cat命令&nl命令
查看>>
.NET底层开发技术
查看>>
RHEL regiester
查看>>
c/c++中的一些基础知识
查看>>
练习:输出整数每一位,计算算数,9出现次数,输出图案,水仙花数
查看>>
操作系统的发展
查看>>
HEVC码流简单分析
查看>>
搭建蚂蚁笔记(服务器)
查看>>
lnmp
查看>>
Oracle教程之Oralce OMF功能详解(三)--使用Oralce OMF管理控制文件
查看>>
C# extern 修饰符的用法
查看>>