-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbe.ts
66 lines (51 loc) · 1.58 KB
/
be.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import express from 'express';
import { IncomingMessage, Server, ServerResponse } from 'http';
const responseString = 'Hello from backend Server';
export interface IBackendServer {
port: number;
server: Server<typeof IncomingMessage, typeof ServerResponse>;
/**
* Returns the HTTP Server corresponding to the Express app.
*
* @public
* @returns {Server<typeof IncomingMessage, typeof ServerResponse>}
*/
getServer(): Server<typeof IncomingMessage, typeof ServerResponse>;
/**
* Closes the express server and returns with the server object.
*
* @public
* @returns {Server<typeof IncomingMessage, typeof ServerResponse>}
*/
close(): Server<typeof IncomingMessage, typeof ServerResponse>;
}
export class BackendServer implements IBackendServer {
port;
server;
constructor(port: number) {
// Initialize parameters
this.port = port;
const app = express();
// Attach parsers
app.use(express.text());
app.use(express.json());
app.get('/ping', (req, res) => {
res.sendStatus(200);
});
app.get('/', (req, res) => {
res.status(200).send(responseString);
});
const server = app.listen(port, () => {
console.log('Backend Server listening on port ' + this.port);
});
this.server = server;
}
public getServer(): Server<typeof IncomingMessage, typeof ServerResponse> {
return this.server;
}
public close(): Server<typeof IncomingMessage, typeof ServerResponse> {
const server = this.server.close();
console.log(`Closed Backend Server with port ${this.port}`);
return server;
}
}