-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
54 lines (43 loc) · 1.54 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import express from "express";
import morgan from "morgan";
import cors from "cors";
import linkRouter from "./routers/link.router";
import homeRouter from "./routers/home.router";
import loginRouter from "./routers/login.router";
import registerRouter from "./routers/register.router";
import connectFlash from "connect-flash";
import { errorHandler, flashHandler } from "./helpers";
import expressSession from "express-session";
import { port } from "./config/conf";
// import difinitions
import "./types/definitions";
// load environment variables
require("dotenv").config();
import { expressSessionConfig } from "./config/sessionConfig";
// initialize express application
const app = express();
// use middlewares
app.use(morgan("tiny")); // logging
app.use(cors()); // cors
app.use(express.urlencoded({ extended: false })); // body parser
app.use(express.json()); // json parser
app.use(expressSession(expressSessionConfig)); // express session
app.use(connectFlash()); // flash messages
app.use(express.static("./views")); // static files
app.use(flashHandler);
// set global variables
app.locals.baseURL = "http://localhost:" + port;
// initialize mongodb connection
import "./config/db";
// set view engine as ejs
app.set("view engine", "ejs");
// render the main view
app.use("/", homeRouter);
// use routers
app.use("/link", linkRouter);
app.use("/login", loginRouter);
app.use("/register", registerRouter);
// error handler
app.use(errorHandler);
// start the application
app.listen(port, () => console.log("Listening on => " + app.locals.baseURL));