-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
56 lines (45 loc) · 1.76 KB
/
app.js
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
55
56
// external imports / dependencies
const express = require("express");
const dotenv = require("dotenv");
const cors = require("cors");
const mongoose = require("mongoose");
const path = require("path");
const cookieParser = require("cookie-parser");
const moment = require("moment");
// middlewares
const { notFoundHandler, errorHandler } = require("./middlewares/errorHandler");
// routes / routers
const loginRouter = require("./routes/loginRouter");
const signupRouter = require("./routes/signupRouter");
const usersRouter = require("./routes/usersRouter");
const inboxRouter = require("./routes/inboxRouter");
const app = express();
dotenv.config();
app.locals.moment = moment;
const port = process.env.PORT;
const mongoConnectionString = process.env.MONGO_URL;
// application config
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.use(cors({ credentials: true }));
app.use(cookieParser(process.env.COOKIE_SECRET));
app.set("view engine", "ejs");
// database connection
mongoose
.connect(mongoConnectionString)
.then(() => console.log("-> database connection successful"))
.catch((err) => console.log(err.message || err));
// for rendering the login page and handling the login related stuffs
app.use("/", loginRouter);
// for rendering the signup page and handling the signup related stuffs
app.use("/signup", signupRouter);
// for rendering the signup page and handling the users related stuffs
app.use("/users", usersRouter);
// for rendering the inbox page and handling the inbox related stuffs
app.use("/inbox", inboxRouter);
// * error handlings
app.use(notFoundHandler);
app.use(errorHandler);
// staring the server
app.listen(port, () => console.log(`-> listening to port [${port}]`));