Skip to content

Instantly share code, notes, and snippets.

@bbachi
Last active February 15, 2019 02:46
Show Gist options
  • Select an option

  • Save bbachi/93556729997dbefea64a25ea75543def to your computer and use it in GitHub Desktop.

Select an option

Save bbachi/93556729997dbefea64a25ea75543def to your computer and use it in GitHub Desktop.
How to write production ready Node.js Rest API - Typescript version
import * as express from 'express';
import * as bodyParser from 'body-parser';
class App {
public express: express.Application;
constructor() {
this.express = express();
this.middleware();
this.routes();
}
// Configure Express middleware.
private middleware(): void {
this.express.use(bodyParser.json());
this.express.use(bodyParser.urlencoded({ extended: false }));
}
private routes(): void {
this.express.use('/', (req,res,next) => {
res.send("Typescript App works!!!");
});
}
}
export default new App().express;
import App from './app';
const port = 3070;
App.set('port', port);
const server = http.createServer(App);
server.listen(port);
server.on('listening', function(): void {
let addr = server.address();
let bind = (typeof addr === 'string') ? `pipe ${addr}` : `port ${addr.port}`;
console.log(`Listening on ${bind}`);
});
module.exports = App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment