aboutsummaryrefslogtreecommitdiff
path: root/src/app.ts
blob: 4f0e0d7bed0ae8cde75f3dcc8f08228497ace520 (plain) (blame)
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
import * as bodyParser from 'body-parser';
import * as ex from 'express';
import FileRouter from './FileRouter';
import * as logger from 'morgan';

class App {
	public express: ex.Express;
	public logEnabled: boolean = true;

	public constructor() {
		this.express = ex();
		this.middleware();
		this.mountRoutes();
	}

	// Configure Express middleware.
	private middleware(): void {
		if (this.logEnabled) {
			this.express.use(logger('combined'));
		}
		this.express.use(bodyParser.json());
		this.express.use(bodyParser.urlencoded({ extended: false }));
	}

	// Configure API endpoints.
	private mountRoutes(): void {
		const defRouter: ex.Router = ex.Router();
		defRouter.get('/', (req: ex.Request, res: ex.Response): void => {
			res.json({
				message: 'Welcome to the CS3099 BE4 server!',
				important : 'Endpoints start from /cs3099group-be-4/'
			});
		});
		this.express.use('/', defRouter);
		this.express.use('/cs3099group-be-4', FileRouter);
	}
}

export default new App().express;