-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.ts
More file actions
70 lines (55 loc) · 2.27 KB
/
app.ts
File metadata and controls
70 lines (55 loc) · 2.27 KB
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
69
70
import * as bodyParser from "body-parser";
import * as express from "express";
import { APILogger } from "./logger/api.logger";
import { TaskController } from "./controller/task.controller";
import swaggerUi = require('swagger-ui-express');
import fs = require('fs');
class App {
public express: express.Application;
public logger: APILogger;
public taskController: TaskController;
/* Swagger files start */
private swaggerFile: any = (process.cwd()+"/swagger/swagger.json");
private swaggerData: any = fs.readFileSync(this.swaggerFile, 'utf8');
private customCss: any = fs.readFileSync((process.cwd()+"/swagger/swagger.css"), 'utf8');
private swaggerDocument = JSON.parse(this.swaggerData);
/* Swagger files end */
constructor() {
this.express = express();
this.middleware();
this.routes();
this.logger = new APILogger();
this.taskController = new TaskController();
}
// Configure Express middleware.
private middleware(): void {
this.express.use(bodyParser.json());
this.express.use(bodyParser.urlencoded({ extended: false }));
}
private routes(): void {
this.express.get('/api/tasks', (req, res) => {
this.taskController.getTasks().then(data => res.json(data));
});
this.express.post('/api/task', (req, res) => {
console.log(req.body);
this.taskController.createTask(req.body.task).then(data => res.json(data));
});
this.express.put('/api/task', (req, res) => {
this.taskController.updateTask(req.body.task).then(data => res.json(data));
});
this.express.delete('/api/task/:id', (req, res) => {
this.taskController.deleteTask(req.params.id).then(data => res.json(data));
});
this.express.get("/", (req, res, next) => {
res.send("Typescript App works!!");
});
// swagger docs
this.express.use('/api/docs', swaggerUi.serve,
swaggerUi.setup(this.swaggerDocument, null, null, this.customCss));
// handle undefined routes
this.express.use("*", (req, res, next) => {
res.send("Make sure url is correct!!!");
});
}
}
export default new App().express;