zoukankan      html  css  js  c++  java
  • [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);
  • 相关阅读:
    (原创)TWR MCF51CN 总线时钟控制和串口测试程序编写
    (原创)360 与 sopc builder 不兼容
    (原创)一步一步学ZedBoard & Zynq(三):使用自带外设IP让ARM PS访问FPGA
    (原创)一步一步学ZedBoard & Zynq(二):使用PL做流水灯
    (原创)一步一步学ZedBoard & Zynq(一):ZedBoard的第一个工程Helloworld
    (原创)由XPS生成AXI Lite 从设备IP模板我们能学到的东西
    [转] 开源硬件时代的挑战和新的机遇
    [转]FPGA工程师:持守梦想or屈于现实
    (原创)K60 的 I2S音频接口设计
    (原创)NIOS中断编程
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4143325.html
Copyright © 2011-2022 走看看