zoukankan      html  css  js  c++  java
  • [Express] Level 2: Middleware -- 2

    Logging Middleware

    Help finish the following middleware code in the logger.js file:

    On the response object, listen to the event that's emitted when the response has been handed off from Express to the underlying Operating System.

      response.on('finish', function () {
        // when event finished
      });

    Inside of the finish callback, calculate the duration of the request by subtracting the startTime from a new Date object. Store the duration in the duration variable, which has already been declared for you.

      response.on('finish', function () {
        duration = +new Date() - startTime;
      });

    Using the stream object, which holds a reference to standard out, write the following message: "This request took ____ ms", where ____ is the duration for the request.

      response.on('finish', function () {
        duration = +new Date() - startTime;
        var message = "This request took "+duration+" ms";
        stream.write(message);
      });

    If we run the code as is, the request will be stuck in our middleware. Call the function that moves processing to the next middleware in the stack.

    next();
    module.exports = function (request, response, next) {
      var startTime = +new Date();
      var stream = process.stdout;
      var duration = null;
    
      response.on('finish', function () {
        duration = +new Date() - startTime;
        var message = "This request took "+duration+" ms";
        stream.write(message);
      });
      
      next();
    };

    Add Logging Middleware

    In the following code in app.js, we require our new middleware and assign it to a variable called logger.

    var express = require('express');
    var app = express();
    
    var logger = require('./logger');
    
    //TODO: mount middleware
    
    app.listen(3000);

    What function should we call in order to mount the middleware and add it to the stack?

    Answer:

    app.use(logger);

    Only GET

    Let's build a middleware that ensures only GET requests are allowed to go through.

    First, in the only_get.js file, create an anonymous function that uses the middleware signature and assign it to module.exports. Remember, the Express middleware function signature takes three arguments.

    module.exports = function(request, response, next){
    
    };

    Use the request object to check if the HTTP method used is 'GET' and if it is, then call the function that moves processing to the next middleware in the stack.

    module.exports = function(request, response, next){
      if(request.method == "GET"){
          next();
      }
    };

    If the HTTP method is not 'GET', then complete the request by sending back a message that says 'Method is not allowed'.

    module.exports = function(request, response, next){
      if(request.method == "GET"){
          next();
      }else{
          response.end('Method is not allowed');
      }
    };

    Buildings 

    var express = require('express');
    var app = express();
    
    app.use(function(request, response, next){
      if (request.path === "/cities"){
        next();
      } else {
        response.status(404).json("Path requested does not exist");
      }
    });
    
    app.get('/cities', function(request, response){
      var cities = ['Caspiana', 'Indigo', 'Paradise'];
      response.json(cities);
    });

    app.listen(3000);

    When we run our previous code and issue a GET request to the /buildings endpoint, what will the response be?

  • 相关阅读:
    http://www.codeplex.com/ 一个不错的开源网站
    阳光灿烂每一天z
    Mercurial使用简单介绍zz
    德国人的记事本 zz
    你一定要知道的71个做饭技巧
    推荐一个原型设计软件 Balsamiq Mockups
    不能一起吃的食物
    雷电预防
    马云 + 史玉柱的40条经典语录zz
    (转)扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4141909.html
Copyright © 2011-2022 走看看