From f6495a7b0fb6fce9066f3032a04232b2000e35c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=A2=A2?= Date: Wed, 16 Jan 2019 15:20:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=91=E7=94=9F=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E5=86=B2=E7=AA=81=20=E5=BA=94=E8=AF=A5=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E5=8F=B7=E8=80=8C=E4=B8=8D=E6=98=AF=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/index.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 16f2dab..53022ee 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -7,6 +7,7 @@ import consola from 'consola' import { createResolve } from 'src/utils/path' import rimraf from 'rimraf' +import http from 'http' import express, { Express, Router } from 'express' import compression from 'compression' import proxyMiddleware from 'http-proxy-middleware' @@ -351,9 +352,35 @@ export function serverStart( } else { options = port || 8020 } - app.listen(options, () => { + + const WAIT_TIME = 1000 + const MAX = 60 + let index = 0 + + const server = http.createServer(app) + server.on('error', function(error) { + if (index++ < MAX) { + consola.fatal( + 'SERVER_START:', + error.message, + `\n\t重试中, 当前次数: ${index}` + ) + setTimeout(() => { + start() + }, WAIT_TIME) + } else { + process.exit(0) + } + }) + server.on('listening', function() { consola.info(`server started at ${JSON.stringify(options, null, 2)}`) }) + + start() + + function start() { + server.listen(options) + } } /**