-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathserver.ts
executable file
·68 lines (58 loc) · 1.48 KB
/
server.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
67
68
import { Injectable } from '@decorators/di';
import { attachControllers, Controller, Get, Post, Response } from '@decorators/express';
import * as express from 'express';
import { Response as Res } from 'express';
@Injectable()
@Controller('/')
class AppController {
private passData = false;
@Post('/login')
public login(@Response() res: express.Response): void {
res.send(this.generateTokens());
}
@Post('/refresh')
public refresh(@Response() res: express.Response): void {
// emulate long request
setTimeout(() => res.send(this.generateTokens()), 1000);
}
@Get('/data')
public getData(@Response() res: Res): void {
this.passData = !this.passData;
if (this.passData) {
res.send([
{
'id': 1,
'name': 'Pepsi',
},
{
'id': 2,
'name': 'Coca-Cola',
},
]);
} else {
// emulate long request
setTimeout(() => res.status(401).send(), 300);
}
}
private generateTokens() {
return {
accessToken: `access-token-${Math.random()}`,
refreshToken: `access-token-${Math.random()}`,
};
}
}
const app = express();
app.use((
_req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
);
next();
});
attachControllers(app, [AppController]);
app.listen(3000);