Created
May 15, 2017 10:52
-
-
Save jalalhejazi/268bfc80cf8ebfcfffc50ba7fb472566 to your computer and use it in GitHub Desktop.
app.ts typescript +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 { json, urlencoded } from "body-parser"; | |
import * as compression from "compression"; | |
import * as express from "express"; | |
import * as path from "path"; | |
import { publicRouter } from "./routes/public"; | |
import { landingPage} from "./routes/index"; | |
import { aboutRouter } from "./routes/about" ; | |
const app: express.Application = express(); | |
app.disable("x-powered-by"); | |
app.use(json()); | |
app.use(compression()); | |
app.use(urlencoded({ extended: true })); | |
// api routes | |
app.use("/", landingPage ) | |
app.use("/api/public", publicRouter); | |
app.use("/about" , aboutRouter ) | |
if (app.get("env") === "production") { | |
// in production mode run application from dist folder | |
app.use(express.static(path.join(__dirname, "/../client"))); | |
} | |
// catch 404 and forward to error handler | |
app.use((req: express.Request, res: express.Response, next) => { | |
const err = new Error("Not Found"); | |
next(err); | |
}); | |
// production error handler | |
// no stacktrace leaked to user | |
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => { | |
res.status(err.status || 500); | |
res.json({ | |
error: {}, | |
message: err.message, | |
}); | |
}); | |
export { app }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment