Create backend server

This commit is contained in:
2022-04-15 00:16:00 +08:00
parent eccd7fdf7c
commit 3220764677
7 changed files with 255 additions and 176 deletions
+8
View File
@@ -0,0 +1,8 @@
import { Service } from "@Service/Service";
import * as minimist from "minimist";
const args = minimist(process.argv.slice(2));
if (args.run) {
new Service().run(args.path, args.port);
}
+47
View File
@@ -0,0 +1,47 @@
import { Express } from "express";
import * as express from "express";
import * as detect from "detect-port";
class Service {
/**
* 默认端口
*/
public servicePort: number = 12100;
/**
* Express 实例
*/
public app!: Express;
/**
* 获取空闲接口
*/
private async getFreePort(): Promise<number> {
let freePort: number = this.servicePort;
try {
freePort = await detect(this.servicePort);
} catch (err) {
console.log(err);
}
return freePort;
}
public async run(url?: string, port?: number) {
this.servicePort = port ?? await this.getFreePort();
this.app = express();
this.app.use("/", express.static(url ?? "./"));
this.app.listen(this.servicePort);
console.log("Service: service run in port " + this.servicePort);
}
}
export { Service };