-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
62 lines (47 loc) · 1.39 KB
/
main.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
// Imports
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 4000;
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, 'logo')));
app.use(bodyParser.urlencoded({ extended: true }));
// Database connection
mongoose.connect(process.env.DB_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on("error", (error) => console.log(error));
db.once("open", () => console.log("Connected to the database"));
// Middlewares
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(
session({
secret: "my secret key",
saveUninitialized: true,
resave: false,
cookie: { secure: false }, // Set to true if using https
})
);
app.use((req, res, next) => {
res.locals.message = req.session.message;
delete req.session.message;
next();
});
// Make uploads directory static
app.use(express.static("uploads"));
app.use(express.static("logo"));
// Set template engine
app.set("view engine", "ejs");
// Router prefix
app.use("", require("./routes/routes"));
// Start server
app.listen(PORT, () => {
console.log(`Server started at http://localhost:${PORT}`);
});