server.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import * as path from "path";
  2. import cookieParser from "cookie-parser";
  3. import * as bodyParser from "body-parser";
  4. import logger from "morgan";
  5. import express from "express";
  6. import rootRouter from "./routes/root";
  7. class App {
  8. public express: express.Application;
  9. constructor() {
  10. this.express = express();
  11. this.routes();
  12. this.middleware();
  13. this.launchConf();
  14. }
  15. private routes(): void {
  16. this.express.use("/", rootRouter);
  17. }
  18. private middleware(): void {
  19. this.express.set("port", process.env.PORT || 3000);
  20. this.express.use(logger("dev"));
  21. this.express.use(express.json());
  22. this.express.use(bodyParser.json());
  23. this.express.use(bodyParser.urlencoded({ extended: false }));
  24. this.express.use(cookieParser());
  25. this.express.use(express.static(path.join(__dirname, "public")));
  26. }
  27. private launchConf() {
  28. const port = this.express.get("port");
  29. console.log('port', port);
  30. this.express.listen(port, () => {
  31. console.log(
  32. ("App is running at http://localhost:%d in %s mode"),
  33. port,
  34. this.express.get("env")
  35. );
  36. console.log("Press CTRL-C to stop");
  37. });
  38. }
  39. }
  40. export default new App().express;