start-server.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.startServer = startServer;
  6. var _log = require("../../build/output/log");
  7. var _http = _interopRequireDefault(require("http"));
  8. var _next = _interopRequireDefault(require("../next"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. function startServer(opts) {
  15. let requestHandler;
  16. const server = _http.default.createServer((req, res)=>{
  17. return requestHandler(req, res);
  18. });
  19. if (opts.keepAliveTimeout) {
  20. server.keepAliveTimeout = opts.keepAliveTimeout;
  21. }
  22. return new Promise((resolve, reject)=>{
  23. let port = opts.port;
  24. let retryCount = 0;
  25. server.on("error", (err)=>{
  26. if (port && opts.allowRetry && err.code === "EADDRINUSE" && retryCount < 10) {
  27. (0, _log).warn(`Port ${port} is in use, trying ${port + 1} instead.`);
  28. port += 1;
  29. retryCount += 1;
  30. server.listen(port, opts.hostname);
  31. } else {
  32. reject(err);
  33. }
  34. });
  35. let upgradeHandler;
  36. if (!opts.dev) {
  37. server.on("upgrade", (req, socket, upgrade)=>{
  38. upgradeHandler(req, socket, upgrade);
  39. });
  40. }
  41. server.on("listening", ()=>{
  42. const addr = server.address();
  43. const hostname = !opts.hostname || opts.hostname === "0.0.0.0" ? "localhost" : opts.hostname;
  44. const app = (0, _next).default({
  45. ...opts,
  46. hostname,
  47. customServer: false,
  48. httpServer: server,
  49. port: addr && typeof addr === "object" ? addr.port : port
  50. });
  51. requestHandler = app.getRequestHandler();
  52. upgradeHandler = app.getUpgradeHandler();
  53. resolve(app);
  54. });
  55. server.listen(port, opts.hostname);
  56. });
  57. }
  58. //# sourceMappingURL=start-server.js.map