aboutsummaryrefslogtreecommitdiff
path: root/src/app.ts
blob: 612ba494c55b13fe5a6bf73912ee99489125adf0 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
	private logEnabled: boolean;

	public constructor(enableLog: boolean) {
		this.express = ex();
		this.middleware();
		this.mountRoutes();
		this.logEnabled = enableLog;
	}

	// Configure Express middleware.
	private middleware(): void {
		if (this.logEnabled) {
			console.log("log enabled")
			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/'
			});
		});
		defRouter.get('/*', (req: ex.Request, res: ex.Response): void => {
			res.status(404)
			.json({
				status: 'error',
				error : 'invalid_route',
				error_description: 'Endpoints start from /cs3099group-be-4/\n'
						+' Please refer to \'https://github.com/CS3099JH2017/cs3099jh/blob/master/protocols/BE01.md\''
						+ ' for further details.'
			});
		})
		this.express.use('/cs3099group-be-4', FileRouter);
		this.express.use('/', defRouter);
	}
}

export default new App(true).express;
export function TestApp(){
	return new App(false).express;
}