Skip to content

Instantly share code, notes, and snippets.

@seanpmaxwell
Last active May 29, 2019 05:25
Show Gist options
  • Save seanpmaxwell/21fb6bde8097749f9fc2fb4568caf19c to your computer and use it in GitHub Desktop.
Save seanpmaxwell/21fb6bde8097749f9fc2fb4568caf19c to your computer and use it in GitHub Desktop.
TypeScriptFullStackShell/src/DemoServer.ts
import * as path from 'path';
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as controllers from './controllers';
import { Server } from '@overnightjs/core';
import { Logger } from '@overnightjs/logger';
class DemoServer extends Server {
private readonly SERVER_START_MSG = 'Demo server started on port: ';
private readonly DEV_MSG = 'Express Server is running in development mode. ' +
'No front-end content is being served.';
constructor() {
super(true);
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({extended: true}));
super.addControllers(new DemoController());
// Point to front-end code
if (process.env.NODE_ENV !== 'production') {
cinfo('Starting server in development mode');
const msg = this._DEV_MSG + process.env.EXPRESS_PORT;
this.app.get('*', (req, res) => res.send(msg));
}
}
private setupControllers(): void {
const ctlrInstances = [];
for (const name in controllers) {
if (controllers.hasOwnProperty(name)) {
let Controller = (controllers as any)[name];
ctlrInstances.push(new Controller());
}
}
super.addControllers(ctlrInstances);
}
public start(port: number): void {
this.app.listen(port, () => {
Logger.Imp(this.SERVER_START_MSG + port);
});
}
}
export default DemoServer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment