<commit_before>const http = require("http");
const createDebug = require('debug');
const chalk = require('chalk');
const Handler = require("./handlers");
const Library = require("./library");

const debug = createDebug('nails:server');
const log = createDebug('nails');

exports = module.exports = class Server {
  constructor(config) {
    this.handler = new Handler(config);
    this.config = config;
  }
  run() {
    this.handler.ready.then(this._run.bind(this));
  }
  _run() {
    debug('starting server...');
    const iface = this.config.server_interface;
    const port = this.config.server_port;
    const server = http.createServer((req, res) => {
      log(req.method, chalk.underline(req.url), `HTTP/${req.httpVersion}`);

      const requestHandler = this.handler.getHandler(req);
      if (requestHandler) {
        const library = new Library();
        const prom = requestHandler.call(library.context, req);
        Promise.resolve(prom).then(() => {
          let opts = library.requestData;

          if (opts != null) {
            if (opts.redirect_to) {
              res.writeHead(302, {
                location: opts.redirect_to,
                'Turbolinks-Location': opts.redirect_to
              });
              res.end();
            }
            else {
              opts = Object.assign(opts, {routes: this.handler.routes});
              Handler.renderer(req, res, opts);
            }
          }
        });
      }
      else {
        Handler.getStaticContent("404.html", this.config).then(customErrorPage => {
          if (customErrorPage) {
            res.statusCode = 404;
            Handler.renderer(req, res, {
              routes: this.handler.routes,
              content: customErrorPage
            });
          }
          else {
            res.writeHead(404, 'Not Found');
            res.end();
          }
          log('404 at', req.url);
        });
      }
    });

    server.listen(port, iface, () => {
      log('Listening on', `${iface}:${port}`);
    });
  }
};<commit_msg>Move request handler to separate method<commit_after>const http = require("http");
const createDebug = require('debug');
const chalk = require('chalk');
const Handler = require("./handlers");
const Library = require("./library");

const debug = createDebug('nails:server');
const log = createDebug('nails');

exports = module.exports = class Server {
  constructor(config) {
    this.handler = new Handler(config);
    this.config = config;
  }
  run() {
    this.handler.ready.then(this._run.bind(this));
  }
  _run() {
    debug('starting server...');
    const iface = this.config.server_interface;
    const port = this.config.server_port;
    const server = http.createServer(this._handleRequest.bind(this));

    server.listen(port, iface, () => {
      console.log("Nails server listening on " + iface + ":" + port);
    });
  }
  _handleRequest(req, res) {
    log(req.method, chalk.underline(req.url), `HTTP/${req.httpVersion}`);

    const requestHandler = this.handler.getHandler(req);
    if (requestHandler) {
      const library = new Library();
      const prom = requestHandler.call(library.context, req);
      Promise.resolve(prom).then(() => {
        let opts = library.requestData;

        if (opts != null) {
          if (opts.redirect_to) {
            res.writeHead(302, {
              location: opts.redirect_to,
              'Turbolinks-Location': opts.redirect_to
            });
            res.end();
          }
          else {
            opts = Object.assign(opts, {routes: this.handler.routes});
            Handler.renderer(req, res, opts);
          }
        }
      });
    }
    else {
      Handler.getStaticContent("404.html", this.config).then(customErrorPage => {
        if (customErrorPage) {
          res.statusCode = 404;
          Handler.renderer(req, res, {
            routes: this.handler.routes,
            content: customErrorPage
          });
        }
        else {
          res.writeHead(404, 'Not Found');
          res.end();
        }
        log('404 at', req.url);
      });
    }
  }
};
{'fraction_non_alphanumeric': 0.14703730797366496, 'fraction_numerical': 0.0073152889539136795, 'mean_word_length': 2.109931766489765, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5645388', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1180, 'n_words': 231}