generated from lielserf/template-Assignment-3.2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
111 lines (94 loc) · 2.95 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
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
require("dotenv").config();
//#region express configures
var express = require("express");
var path = require("path");
var logger = require("morgan");
const session = require("client-sessions");
const DButils = require("./routes/utils/DButils");
var cors = require('cors')
var app = express();
app.use(logger("dev")); //logger
app.use(express.json()); // parse application/json
app.use(
session({
cookieName: "session", // the cookie key name
//secret: process.env.COOKIE_SECRET, // the encryption key
secret: "template", // the encryption key
duration: 24 * 60 * 60 * 1000, // expired after 20 sec
activeDuration: 1000 * 60 * 5, // if expiresIn < activeDuration,
cookie: {
httpOnly: false,
}
//the session will be extended by activeDuration milliseconds
}),
// cors({
// credentials: true,
// origin: [
// 'http://localhost:3000',
// 'http://localhost:8080'
// ]
// }),
);
app.use(express.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(express.static(path.join(__dirname, "public"))); //To serve static files such as images, CSS files, and JavaScript files
//local:
// app.use(express.static(path.join(__dirname, "dist")));
//remote:
app.use(express.static(path.join(__dirname, '../assignment3-3-312804446_319081600/dist'))); //C:\Users\user\Desktop\assignment3-3-312804446_319081600\dist\index.html
app.get("/",function(req,res)
{
//remote:
res.sendFile(path.join(__dirname, '../assignment3-3-312804446_319081600/dist/index.html'));
//local:
// res.sendFile(__dirname+"/index.html");
});
// app.use(cors());
// app.options("*", cors());
const corsConfig = {
origin: true,
credentials: true
};
app.use(cors(corsConfig));
app.options("*", cors(corsConfig));
var port = process.env.PORT || "3000"; //local=3000 remote=80
//#endregion
const user = require("./routes/user");
const recipes = require("./routes/recipes");
const auth = require("./routes/auth");
//#region cookie middleware
app.use(function (req, res, next) {
if (req.session && req.session.user_id) {
DButils.execQuery("SELECT user_id FROM users")
.then((users) => {
if (users.find((x) => x.user_id === req.session.user_id)) {
req.user_id = req.session.user_id;
}
next();
})
.catch((error) => next());
} else {
next();
}
});
//#endregion
// ----> For cheking that our server is alive
app.get("/alive", (req, res) => res.send("I'm alive"));
// Routings
app.use("/users", user);
app.use("/recipes", recipes);
app.use(auth);
// Default router
app.use(function (err, req, res, next) {
console.error(err);
res.status(err.status || 500).send({ message: err.message, success: false });
});
// const server = app.listen(port, () => {
// console.log(`Server listen on port ${port}`);
// });
// process.on("SIGINT", function () {
// if (server) {
// server.close(() => console.log("server closed"));
// }
// process.exit();
// });
module.exports=app;