diff options
Diffstat (limited to 'src/app.ts')
| -rw-r--r-- | src/app.ts | 44 |
1 files changed, 42 insertions, 2 deletions
@@ -13,10 +13,31 @@ import { default as File } from './db/model/File'; import { default as Project } from './db/model/Project'; import { default as User } from './db/model/User'; +/** + * Configuration of the Express server app, including installation of + * various middleware functions. + */ + +/** + * The Class used to configure the Express app used in the server instance. + */ class App { + /** + * The Express app used throught the server. + */ public express: ex.Express; + + /** + * Whether or not logging is enabled on the server. + */ private readonly logEnabled: boolean = true; + /** + * Constructs an instance of the app, by loading middleware. + * + * @param enableLog Whether or not logging should be enabled on the + * server. + */ public constructor(enableLog: boolean) { this.logEnabled = enableLog; this.express = ex(); @@ -35,7 +56,9 @@ class App { this.errorware(); } - // Configure Express middleware. + /** + * Configures regular Express middleware. + */ private middleware(): void { this.express.use(expressJwt({secret: 'Mr Secret'}) .unless({path: [ @@ -51,7 +74,9 @@ class App { this.express.use(bodyParser.urlencoded({ extended: false })); } - // Configure API endpoints. + /** + * Adds the Router routes to the Express app. + */ private mountRoutes(): void { const defRouter: ex.Router = ex.Router(); defRouter.get('/', (req: ex.Request, res: ex.Response): void => { @@ -75,11 +100,22 @@ class App { this.express.use('/', defRouter); } + /** + * Adds error-handling (4-arity) middleware to the Express app. + */ private errorware(): void { this.express.use(unauthorisedErr); this.express.use(errorHandler); } + /** + * Introduces variable-preconditioning middleware to the Express app. + * + * These middleware functions pre-process Express' matching variables + * in routes (e.g. `:project_name`). + * If an entity cannot be found in the database, the corresponding + * value is set to `null` instead. + */ private precondition(): void { // Fetch a user from their name: this.express.param( @@ -197,6 +233,10 @@ class App { } export default new App(true).express; + +/** + * A version of the app used only for testing. + */ export function TestApp() { return new App(false).express; } |
