/
PostController.ts
48 lines (35 loc) · 1.17 KB
/
PostController.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
/**
* Example handling asynchronous calls for the OvernightJS
* library with custom router object. This example is setup
* for the express-promise-router library.
*
* created by Sean Maxwell Aug 28, 2018
*/
import { Request, Response } from 'express';
import { Controller, Get, Put } from '@overnightjs/core';
@Controller('api/posts')
class PostController {
@Get(':id')
private get(req: Request, res: Response): Promise<Response> {
return this.someAsyncFunction(req.params.id)
.then((ret) => res.status(200).json({msg: ret}));
}
private someAsyncFunction(id: number): Promise<string> {
return new Promise((resolve, reject) => {
if (isNaN(id)) {
reject('You entered an invalid post id: ' + id);
} else {
resolve('You entered the post id: ' + id);
}
});
}
@Put(':id')
private add(req: Request, res: Response): Promise<string> {
return Promise.resolve('next');
}
@Put('foo')
private add2(req: Request, res: Response): void {
res.status(200).json({msg: 'Route used: ' + req.url});
}
}
export default PostController;