Last active
February 15, 2019 02:46
-
-
Save bbachi/93556729997dbefea64a25ea75543def to your computer and use it in GitHub Desktop.
How to write production ready Node.js Rest API - Typescript version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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