-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
main.ts
49 lines (38 loc) 路 1.04 KB
/
main.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
import * as express from "express"
declare var __DEV__: boolean;
export class Server {
public app: express.Express;
public port: number;
constructor() {
this.app = express();
this.port = this.getPort();
this.setRoutes();
this.start();
}
private start = (): void => {
this.app.listen(this.port, this.onListen);
}
private onListen = (err: any): void => {
if (err) {
console.error(err)
return;
}
if (__DEV__) {
console.log('> in development')
}
console.log(`> listening on port ${this.port}`)
};
private getPort = (): number => parseInt(process.env.PORT, 10) || 3000;
private setRoutes = (): void => {
this.app.get('/', this.getHomepage);
}
private async getHomepage(req: express.Request, res: express.Response): Promise<express.Response> {
try {
const thing = await Promise.resolve({ one: 'two' })
return res.json({ ...thing, hello: 'world' })
} catch (e) {
return res.json({ error: e.message })
}
};
}
module.exports = new Server().app;