generated from acttoreact/ts-node-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
50 lines (40 loc) · 984 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import path from 'path';
import { ServerResponse } from './model/server';
import { isJest } from './tools/isJest';
import createServer from './utils/createServer';
import { defaultPort, targetPath, apiPath } from './settings';
const defaultServerApiPath = path.resolve(__dirname, targetPath, apiPath);
let server: ServerResponse | null = null;
/**
* Returns server response instance
*/
export const getServer = (): ServerResponse | null => {
return server;
};
/**
* Stop server (if running)
*/
export const stop = async (): Promise<void> => {
if (server) {
await server.close();
server = null;
}
};
/**
* Start of the project
*/
export const start = async (
port = defaultPort,
serverApiPath = defaultServerApiPath,
): Promise<ServerResponse> => {
await stop();
server = await createServer(port, serverApiPath);
return server;
};
/**
* Stops server (if running) and starts again
*/
export const restart = start;
if (!isJest()) {
start();
}