Create backend server
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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 };
|
||||
Reference in New Issue
Block a user