Skip to content

Commit 2c69cf5

Browse files
committed
解决端口占用问题
1 parent 75a01de commit 2c69cf5

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,27 +200,36 @@ server$1.use(middlewares);
200200
var createServer = function (option, callback) {
201201
var config = option.https;
202202
config = /^(boolean|number)$/.test(typeof config) ? config && {} : config;
203+
var currentServer;
203204
if (config instanceof Object) {
204205
if (typeof config.key !== 'string' || typeof config.cert !== 'string' || config.key.length + config.cert.length === 0) {
205206
config.key = fs.readFileSync(path.join(__dirname, 'ssl/key.pem'));
206207
config.cert = fs.readFileSync(path.join(__dirname, 'ssl/cert.pem'));
207208
console.log("正在使用默认的证书配置");
208209
}
209-
https.createServer(config, server$1).listen(option.port, function () {
210+
currentServer = https.createServer(config, server$1).listen(option.port, function () {
210211
console.log();
211212
console.log("\u5DF2\u542F\u52A8json-server\u670D\u52A1\u5668 https://localhost:" + option.port);
212213
console.log();
213214
typeof callback == 'function' && callback();
214215
});
215216
}
216217
else {
217-
server$1.listen(option.port, function () {
218+
currentServer = server$1.listen(option.port, function () {
218219
console.log();
219220
console.log("\u5DF2\u542F\u52A8json-server\u670D\u52A1\u5668 http://localhost:" + option.port);
220221
console.log();
221222
typeof callback == 'function' && callback();
222223
});
223224
}
225+
currentServer.on('error', function () {
226+
var rest = [];
227+
for (var _i = 0; _i < arguments.length; _i++) {
228+
rest[_i] = arguments[_i];
229+
}
230+
option.port++;
231+
createServer(option, callback);
232+
});
224233
};
225234
/**
226235
* 启动mock服务

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ks-mock",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "mock server 模拟后端API接口",
55
"main": "index.js",
66
"scripts": {

src/server.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,31 @@ server.use(middlewares)
1818
const createServer = (option: anyObject, callback?: Function) => {
1919
let config = option.https
2020
config = /^(boolean|number)$/.test(typeof config) ? config && {} : config
21+
let currentServer
2122
if (config instanceof Object) {
2223
if (typeof config.key !== 'string' || typeof config.cert !== 'string' || config.key.length + config.cert.length === 0) {
2324
config.key = fs.readFileSync(path.join(__dirname, 'ssl/key.pem'))
2425
config.cert = fs.readFileSync(path.join(__dirname, 'ssl/cert.pem'))
2526
console.log("正在使用默认的证书配置")
2627
}
27-
https.createServer(config, server).listen(option.port, function () {
28+
currentServer = https.createServer(config, server).listen(option.port, function () {
2829
console.log()
2930
console.log(`已启动json-server服务器 https://localhost:${option.port}`)
3031
console.log()
3132
typeof callback == 'function' && callback();
3233
})
3334
} else {
34-
server.listen(option.port, () => {
35+
currentServer = server.listen(option.port, () => {
3536
console.log()
3637
console.log(`已启动json-server服务器 http://localhost:${option.port}`)
3738
console.log()
3839
typeof callback == 'function' && callback();
3940
})
4041
}
42+
currentServer.on('error', (...rest) => {
43+
option.port++
44+
createServer(option, callback)
45+
})
4146
}
4247
/**
4348
* 启动mock服务
@@ -257,6 +262,6 @@ const Server = (option: anyObject, callback?: Function) => {
257262
res.status(200).jsonp(body)
258263
}
259264
server.use(router)
260-
createServer(option, callback);
265+
createServer(option, callback)
261266
}
262267
export default Server

0 commit comments

Comments
 (0)