[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);
复制代码

 

posted @   Zhentiw  阅读(360)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示