Skip to content

Commit

Permalink
feat: 发生端口冲突 应该重试端口号而不是直接关闭
Browse files Browse the repository at this point in the history
  • Loading branch information
孙颢 committed Jan 16, 2019
1 parent d35db44 commit f6495a7
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
}
}

/**
Expand Down

0 comments on commit f6495a7

Please sign in to comment.