-
-
Save zburgermeiszter/fa16372e56b75bede55c69a19a9ba404 to your computer and use it in GitHub Desktop.
Express Server with TypeScript
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 server from "./server"; | |
new server.App |
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 server from "./server"; | |
new server.App('production',9090); |
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 path from "path" // normalize the paths : http://stackoverflow.com/questions/9756567/do-you-need-to-use-path-join-in-node-js | |
import express = require('express'); | |
export class Routes { | |
protected basePath: string; | |
protected api: database.API; | |
constructor(NODE_ENV: string){ | |
switch(NODE_ENV) { | |
case 'production': | |
this.basePath = '/app/dist'; | |
break; | |
case 'development': | |
this.basePath = '/app/public'; | |
break; | |
} | |
} | |
defaultRoute(req: express.Request, res: express.Response){ | |
res.sendFile('index1.html', {root: path.join(process.cwd(), this.basePath)}); | |
} | |
paths(app: express.Application) { | |
app.get('/', (req: express.Request, res: express.Response) => { | |
this.defaultRoute(req, res); | |
}); | |
app.get('*', (req: express.Request, res: express.Response) => { | |
this.defaultRoute(req, res); | |
}); | |
} | |
} |
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
// set up ======================== | |
import * as express from "express"; | |
import * as morgan from "morgan"; // log requests to the console (express4) | |
import * as path from "path"; // normalize the paths : http://stackoverflow.com/questions/9756567/do-you-need-to-use-path-join-in-node-js | |
import * as bodyParser from "body-parser"; // pull information from HTML POST (express4) | |
import * as methodOverride from "method-override"; // simulate DELETE and PUT (express4) | |
import * as helmet from "helmet"; // Security | |
import * as compression from "compression"; | |
import * as routes from "./routes"; | |
export class App { | |
protected app: express.Application; | |
constructor(NODE_ENV: string = 'development', PORT: number = 8080){ | |
/** | |
* Setting environment for development|production | |
*/ | |
process.env.NODE_ENV = process.env.NODE_ENV || NODE_ENV; | |
/** | |
* Setting port number | |
*/ | |
process.env.PORT = process.env.PORT || PORT; | |
/** | |
* Create our app w/ express | |
*/ | |
this.app = express(); | |
this.app.use(helmet()); | |
if(NODE_ENV === 'development'){ | |
this.app.use(express.static(path.join(process.cwd(), 'public'))); | |
this.app.use('/bower_components', express.static(path.join(process.cwd(), 'bower_components'))); // set the static files location of bower_components | |
this.app.use(morgan('dev')); // log every request to the console | |
}else{ | |
this.app.use(compression()); | |
this.app.use(express.static(path.join(process.cwd(), 'dist'), { maxAge: '7d' })); // set the static files location /public/img will be /img for users | |
} | |
this.app.use(bodyParser.urlencoded({'extended':true})); // parse application/x-www-form-urlencoded | |
this.app.use(bodyParser.json()); // parse application/json | |
this.app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json | |
this.app.use(methodOverride()); | |
/** | |
* Setting routes | |
*/ | |
let __routes = new routes.Routes(process.env.NODE_ENV); | |
__routes.paths(this.app); | |
/** | |
* START the server | |
*/ | |
this.app.listen(process.env.PORT, function(){ | |
console.log('The server is running in port localhost: ', process.env.PORT); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment