-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
35 lines (30 loc) · 1006 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import express from 'express';
import * as path from 'path';
import { isFreePort } from 'find-free-ports';
import http from 'http';
import logger from './logger';
async function startApp() {
logger.info('starting the application...');
const randomPort = await (() => {
return new Promise<number>(async (resolve) => {
for (let port = 3000; port <= 5000; port++) {
if (await isFreePort(port)) {
return resolve(port);
}
}
});
})();
const PORT = parseInt(process.env.PORT as string, 10) || randomPort;
return new Promise<http.Server>((resolve) => {
const app = express();
const server = http.createServer(app);
app.use(express.json());
app.use(express.static(path.join(__dirname, '../public')));
server.listen(PORT, () => {
console.log('server is running on host `http://localhost:' + PORT + '`.');
logger.info('the application started successfully.');
resolve(server);
});
});
}
export default startApp;