-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·132 lines (116 loc) · 2.99 KB
/
server.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* SETUP DEPENDENCY:
*/
import { json, urlencoded } from "body-parser";
import cookieParser from "cookie-parser";
import {
greenBright,
bgGreen,
redBright,
whiteBright,
bgRed,
yellowBright,
cyan,
} from "chalk";
import express, { static } from "express";
import flash from "connect-flash";
import morgan from "morgan";
import { connect, connection } from "mongoose";
import { join } from "path";
import passport, { initialize, session as _session } from "passport";
import session from "express-session";
const MongoStore = require("connect-mongo")(session);
// ===CONFIG MIDDLEWARE:
require("./config/passport").default(passport);
// ===SETTINGS:
const log = console.log;
const app = express();
const PORT = process.env.PORT || 2019;
// ===CONNECT DATABASE MONGODB:
const MONGODB_URI =
"mongodb://func_admin:[email protected]:47207/heroku_wzkkq1xr";
connect(MONGODB_URI, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() =>
log(greenBright("Database connection ") + bgGreen.bold("SUCCESS"))
)
.catch((err) => log(redBright(err)));
const database = connection;
database.on("error", (err) => {
log(
whiteBright("Database connection ") +
bgRed.bold("ERROR\n") +
redBright(err.message)
);
});
// ===EJS TEMPLATE:
app.set("views", join(__dirname, "views"));
app.set("view engine", "ejs");
// ===GET STATIC FILES:
app.use(static(join(__dirname, "static")));
// ===EXPRESS BODY PARSER:
app.use(morgan("dev"));
app.use(json());
app.use(
urlencoded({
extended: false,
})
);
app.use(cookieParser());
// ===EXPRESS SESSION:
app.use(
session({
secret: "Good-Jobs",
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 60000,
secure: false,
},
store: new MongoStore({
mongooseConnection: database,
}),
})
);
// ===PASSPORT MIDDLEWARE:
app.use(initialize());
app.use(_session());
// ===CONNECT FLASH:
app.use(flash());
app.use((req, res, next) => {
res.locals.current_user = req.user;
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
next();
});
// ===GET ROUTERS:
app.use("/", require("./routes/index").default);
app.use("/users", require("./routes/users").default);
// app.use('/hunters', require('./routes/hunters'));
// ===CATCH 404 Page:
app.use((_req, res, _next) => {
res.status(404);
res.render("pages/404");
});
// ===ERROR HANDLER:
app.use((err, _req, res, _next) => {
res.status(err.status || 500);
res.send(err.message);
});
// RUNNING SERVER ==>
app.listen(PORT, () => {
log(
whiteBright("SERVER STARTED, CLICK [Ctrl] + ") +
yellowBright(`http://localhost:${PORT}`)
);
log(
`${whiteBright(" Press ") + cyan("[Ctrl] + [C]")} to ${bgRed.bold(
"STOP\n"
)}`
);
});
export default app;