NodejsにH 5 Historyモード(connect-history-api-fallbackソースコード解析)をサポートさせる

5547 ワード

リード
本文は主にconnect-history-api-fallbackライブラリに対してソースコード分析を行った.connect-history-api-fallbackは、SPA Historyルーティングモードをサポートするためのnodejsライブラリである.本文を読む前に、HTML5 Historyモードに対応してある程度の理解があります!
ソース分析
/** 
 *       history  ,     url         api       , localhost:4200/home  url,        “     html,      ”,         。
 *           ,                。
 * connect-history-api-fallback                    ,           !
 *                 ,       ,   !
 */

'use strict';

var url = require('url');

exports = module.exports = function historyApiFallback(options) {
  //       
  options = options || {};
  //         
  var logger = getLogger(options);

  //             ,     req, res, next
  return function(req, res, next) {
    var headers = req.headers;
    if (req.method !== 'GET') {
      //         GET  ,       html,     next(),           
      logger(
        'Not rewriting',
        req.method,
        req.url,
        'because the method is not GET.'
      );
      return next();
    } else if (!headers || typeof headers.accept !== 'string') {
      //        ,       accept     ,         http  ,     ,           
      logger(
        'Not rewriting',
        req.method,
        req.url,
        'because the client did not send an HTTP accept header.'
      );
      return next();
    } else if (headers.accept.indexOf('application/json') === 0) {
      //          application/json     ,        html,     ,           
      logger(
        'Not rewriting',
        req.method,
        req.url,
        'because the client prefers JSON.'
      );
      return next();
    } else if (!acceptsHtml(headers.accept, options)) {
      //             Accept     ['text/html', '*/*'],          html,     ,           
      logger(
        'Not rewriting',
        req.method,
        req.url,
        'because the client does not accept HTML.'
      );
      return next();
    }

    //           html ,       

    //     url   parse     url,       ,  protocol,hash,path, pathname, query, search   ,      location  
    var parsedUrl = url.parse(req.url);
    var rewriteTarget;
    //         rewrites,        ;
    //           ,      from to    ;
    // from       pathname        ;
    // to      url,to        ,                  ,             context,context      (parsedUrl,match,request)
    options.rewrites = options.rewrites || [];
    //          
    for (var i = 0; i < options.rewrites.length; i++) {
      var rewrite = options.rewrites[i];
      //       match     
      var match = parsedUrl.pathname.match(rewrite.from);
      if (match !== null) {
        //   match  null,  pathname          
        rewriteTarget = evaluateRewriteRule(parsedUrl, match, rewrite.to, req);

        if(rewriteTarget.charAt(0) !== '/') {
          //     /            url
          logger(
            'We recommend using an absolute path for the rewrite target.',
            'Received a non-absolute rewrite target',
            rewriteTarget,
            'for URL',
            req.url
          );
        }

        logger('Rewriting', req.method, req.url, 'to', rewriteTarget);
        //      url  
        req.url = rewriteTarget;
        return next();
      }
    }

    var pathname = parsedUrl.pathname;
    //       :           url    .  , .         ,   history          
    //           “      ”
    // disableDotRule true,          
    if (pathname.lastIndexOf('.') > pathname.lastIndexOf('/') &&
        options.disableDotRule !== true) {
      //   pathname     /    .,      /a/b/c/d.*   (*        );
      //       disableDotRule false,          ,      ,       
      logger(
        'Not rewriting',
        req.method,
        req.url,
        'because the path includes a dot (.) character.'
      );
      return next();
    }

    //   pathname    /    .,  disableDotRule true,        :  url
    //   url    /index.html,         index   
    rewriteTarget = options.index || '/index.html';
    logger('Rewriting', req.method, req.url, 'to', rewriteTarget);
    //   url
    req.url = rewriteTarget;
    //                (url   index.html ,                ,       html        ,    !)
    next();
  };
};

//          to
function evaluateRewriteRule(parsedUrl, match, rule, req) {
  if (typeof rule === 'string') {
    //       ,    
    return rule;
  } else if (typeof rule !== 'function') {
    //       ,    
    throw new Error('Rewrite rule can only be of type string or function.');
  }

  //           ,        url
  return rule({
    parsedUrl: parsedUrl,
    match: match,
    request: req
  });
}

//       accept                   
function acceptsHtml(header, options) {
  options.htmlAcceptHeaders = options.htmlAcceptHeaders || ['text/html', '*/*'];
  for (var i = 0; i < options.htmlAcceptHeaders.length; i++) {
    if (header.indexOf(options.htmlAcceptHeaders[i]) !== -1) {
      return true;
    }
  }
  return false;
}

//     
function getLogger(options) {
  if (options && options.logger) {
    //           ,          
    return options.logger;
  } else if (options && options.verbose) {
    //   ,     verbose,    console.log      
    return console.log.bind(console);
  }
  //          ,       
  return function(){};
}