-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (45 loc) · 1.27 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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
const cookieSession = require("cookie-session");
const cookieParser = require("cookie-parser");
const router = require("./router/index");
const errorMiddleware = require("./middlewares/error-middleware");
const config = require("./config");
const app = express();
const PORT = config.PORT || 5000;
app.use(cors());
app.use(express.json());
app.use(cookieParser());
app.use(bodyParser.json());
app.use("/api", router);
app.use(errorMiddleware); // middleware должен всегда быть подключен в конце
app.use(
cookieSession({
name: "session",
keys: ["this's my secret"],
// Cookie Options
maxAge: 24 * 60 * 60 * 1000, // 24 hours
})
);
const start = async () => {
try {
app.listen(PORT, () =>
console.log(`server was running on PORT = ${config.PORT}`)
);
} catch (e) {
console.log(e);
}
};
start();
mongoose.connect(config.DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection.on("connected", () => {
console.log("Connect to DB");
});
mongoose.connection.on("error", (err) => {
console.log("Not succesfull conection to database" + err);
});