-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.ts
41 lines (34 loc) · 1.16 KB
/
App.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
import * as restify from 'restify';
export class App {
protected server: restify.Server;
constructor() {
// Init the server
this.initServer();
}
private initServer(): void {
// Create a basic restify server
this.server = restify.createServer();
// Add a middleware to log requests
this.server.use((req, res, next) => {
(<any> req)._start = Date.now();
next();
});
// Add a request handler for the home page
this.server.get('/', (req, res, next) => {
res.send('Hello world !');
next();
});
this.server.on('after', (req, res) => {
const duration = typeof (<any> req)._start === 'number' ? Date.now() - (<any> req._start) : 0;
// tslint:disable-next-line:no-console
console.log(`${req.method} ${req.url} - ${res.statusCode} - ${duration}ms`);
});
}
public start(port: number): void {
// Start the server
this.server.listen(port, () => {
// tslint:disable-next-line:no-console
console.log(`Server started on port ${port}`);
});
}
}