초기화 함수 init(4)

김대웅·2021년 7월 13일
0

express 분석

목록 보기
8/14

express/application/subdomain

app.init = function init() {
  this.cache = {};
  this.engines = {};
  this.settings = {};

  this.defaultConfiguration();
};

/**
 * Initialize application configuration.
 * @private
 */

app.defaultConfiguration = function defaultConfiguration() {
  var env = process.env.NODE_ENV || "development";

  // default settings
  this.enable("x-powered-by");
  this.set("etag", "weak");
  this.set("env", env);
  this.set("query parser", "extended");
  this.set("subdomain offset", 2);
  this.set("trust proxy", false);

  /**
   * settings개체에 기본 값을 설정한다.
   *
   * x-powered-by : true
   * etag : "weak"
   * etag fn : ETag생성 함수
   * env : "development"
   * query parser : "extended"
   * query parser fn : 함수
   * subdomain offset : 2
   * trust proxy : false
   * trust proxy fn : 함수
   */

  /**
   * subdomain offset : subdomain segment의 시작을 결정한다.
   *
   * 도메인 구성
   *
   * 1. Top-level domain(TLD) -> .com / .org
   *
   * 2. Second-level domain(SLD) -> naver / google
   *
   * subdomain
   * SLD앞에 오는것
   * 가장 공통적인 subdomain은 www이다.
   *
   * subdomain의 예: m.naver.com -> 'm'
   */

  /**
   * subdomain offset은 TLD부터 카운팅 하는것 같다.
   * TLD(0) SLD(1) subdomain(2) 순서라 subdomain offset은 기본 2로 설정되는듯
   *
   * test2.localhost:4000과 같이 TLD가 없고, subdomain offset이 기본값(2)으로 설정되어 있는경우 subdomain을 가져올 수 없다고 한다.
   *
   * https://stackoverflow.com/questions/29146489/how-to-get-local-subdomains-using-express-js-s-req-subdomains 참고
   */

  /**
   * https://blog.hubspot.com/what-is-a-domain
   *
   * http:// -> 프로토콜
   *
   * blog. -> subdomain
   *
   * hubspot -> SLD
   *
   * .com -> TLD
   *
   * hubspot.com -> domain name
   *
   * /what-is-a-domain -> page path
   *
   * https://blog.hubspot.com/website/what-is-a-subdomain?toc-variant-a= 참고
   */

  // trust proxy inherit back-compat
  Object.defineProperty(this.settings, trustProxyDefaultSymbol, {
    configurable: true,
    value: true,
  });

  debug("booting in %s mode", env);

  this.on("mount", function onmount(parent) {
    // inherit trust proxy
    if (
      this.settings[trustProxyDefaultSymbol] === true &&
      typeof parent.settings["trust proxy fn"] === "function"
    ) {
      delete this.settings["trust proxy"];
      delete this.settings["trust proxy fn"];
    }

    // inherit protos
    setPrototypeOf(this.request, parent.request);
    setPrototypeOf(this.response, parent.response);
    setPrototypeOf(this.engines, parent.engines);
    setPrototypeOf(this.settings, parent.settings);
  });

  // setup locals
  this.locals = Object.create(null);

  // top-most app is mounted at /
  this.mountpath = "/";

  // default locals
  this.locals.settings = this.settings;

  // default configuration
  this.set("view", View);
  this.set("views", resolve("views"));
  this.set("jsonp callback name", "callback");

  if (env === "production") {
    this.enable("view cache");
  }

  Object.defineProperty(this, "router", {
    get: function () {
      throw new Error(
        "'app.router' is deprecated!\nPlease see the 3.x to 4.x migration guide for details on how to update your app."
      );
    },
  });
};
profile
42seoul cadet

0개의 댓글