-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (57 loc) · 1.88 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
const express = require("express");
const cors = require("cors");
const cookieSession = require("cookie-session");
const dbConfig = require("./config/db.config");
const authConfig = require("./config/auth.config");
const app = express();
// app.use(cors({
// credentials: true,
// exposedHeaders: '*',
// allowedHeaders: 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
// preflightContinue: false,
// origin: true,
// methods: ['GET', 'POST', 'DELETE', 'UPDATE', 'PUT', 'PATCH', 'OPTIONS', 'HEAD']
// }))
// app.options(
// '*',
// cors({
// credentials: true,
// exposedHeaders: '*',
// allowedHeaders: 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
// preflightContinue: false,
// origin: true,
// methods: ['GET', 'POST', 'DELETE', 'UPDATE', 'PUT', 'PATCH', 'OPTIONS', 'HEAD']
// })
// );
app.use(cors());
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
app.use(
cookieSession({
name: "connect.sid",
secret: authConfig.cookieSecret, // should use as secret environment variable
httpOnly: true
})
);
// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to the LearnifyServer api application." });
});
// routes
require("./routes/auth.routes")(app);
require("./routes/set.routes")(app);
require("./routes/user.routes")(app);
const start = async () => {
try {
console.log("Connecting to database...");
const db = require("./models");
await db.mongoose.connect(`mongodb://${dbConfig.HOST}:${dbConfig.PORT}/${dbConfig.DB}`, { useNewUrlParser: true, useUnifiedTopology: true });
app.listen(8080, () => console.log(`App started on 8080!`));
} catch (error) {
console.error(error);
process.exit(1);
}
};
start();