-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
107 lines (76 loc) · 2.7 KB
/
server.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//requiring some stuff for our app
import { NextFunction, Request, Response} from "express";
import expressError from "./utils/expressError";
import express from "express";
import dotenv from "dotenv";
import path from "path";
import flash from "connect-flash";
//creating express app
const app = express();
// Set up middlewares
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.set('view engine','ejs');
app.set('views', path.join(__dirname,'../views'));
app.use(express.static('public'));
//importing sessions & Declaring merging on express-session
import sessions from 'express-session';
declare module 'express-session' {
export interface SessionData {
active_user_nickname: { [key: string]: any };
active_user_email: { [key:string]: any };
active_user_id: { [key: number]: any };
}
}
//requiring mosgoStore for storing our sessions in mongo Atlas DB
import mongostore from "connect-mongo";
//dontenv configured and requiring db_handler
dotenv.config();
import { db_handler } from "./database/config";
//get connection to our DB
db_handler.authenticate().then(() => {
console.log("Successfully connected to MySQL server");
}).catch((error: Error) => {
console.error('Ouups, cannot get connection to MySQL server! ' + error.message);
});
// /** SETUP OUR SESSIONS */
const sessionOption = {
name: String(process.env.SESSION_NAME),
secret: String(process.env.SESSION_SECRET),
resave: false,
saveUninitialized: false,
store: mongostore.create({
mongoUrl: String(process.env.MONGO_ATLAS_SESSION_STORE_URL),
touchAfter: 5 * 24 * 60 * 60,
}),
cookie: {
maxAge: 5 * 24 * 60 * 60 * 1000, // max_age = 5 days
httpOnly: true,
}
};
app.use(sessions(sessionOption));
app.use(flash());
app.use((req:Request, res:Response, next:Function) => {
res.locals.success = req.flash("success");
res.locals.danger = req.flash("danger");
res.locals.active_user_email = req.session.active_user_email;
res.locals.active_user_id = req.session.active_user_id;
res.locals.active_user_nickname=req.session.active_user_nickname;
next()
})
//use routes
import Router from "./routes/index";
app.use(Router);
//errors handling middlwares
app.all('*',(req:Request,res:Response,next:NextFunction)=>{
next(new expressError("Not Found",404));
})
app.use((err:expressError,req:Request,res:Response,next:NextFunction) =>{
const {statusCode=500,message='Something went wrong'}=err;
res.status(statusCode).send(message);
});
//make our app listen on port 3000
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("app runs on [port:", PORT, "]");
});