server.ts 1.3 KB

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