-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
102 lines (83 loc) · 2.44 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
const express = require("express");
const mongoose = require("mongoose");
const fileUpload = require("express-fileupload");
const dotenv = require("dotenv");
const morgan = require("morgan");
const rfs = require("rotating-file-stream");
const path = require("path");
const cors = require("cors");
const app = express();
app.set("trust proxy", "127.0.0.1");
const userRoutes = require("./routes/user");
const courseRoutes = require("./routes/course");
const listRoutes = require("./routes/list");
const authRoutes = require("./routes/auth");
var accessLogStream = rfs.createStream("access.log", {
interval: "1d",
path: path.join(__dirname, "log"),
});
dotenv.config();
// Database
var dbURL = process.env.db;
if (!dbURL) {
var user = process.env.mongoUser;
var password = process.env.mongoPassword;
var host = process.env.mongoHost;
var db = process.env.mongoDatabase;
dbURL = `mongodb://${user}:${password}@${host}/${db}?authSource=admin`;
}
mongoose
.connect(dbURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to database..."))
.catch((err) => console.error(err));
mongoose.set("useCreateIndex", true);
var allowedOrigins = ["http://localhost", "http://127.0.0.1", "http://0.0.0.0"];
// Middleware
app.use(
cors({
origin: function (origin, callback) {
if (!origin) return callback(null, true);
origin = origin.substring(0, origin.lastIndexOf(":"));
if (allowedOrigins.indexOf(origin) === -1) {
var msg =
"The CORS policy of this site does not allow access from the specified origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
},
})
);
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(
fileUpload({
preserveExtension: true,
// safeFileNames: true
})
);
app.use(morgan("combined", { stream: accessLogStream }));
// Routes
app.use("/user", userRoutes);
app.use("/course", courseRoutes);
app.use("/list", listRoutes);
app.use("/auth", authRoutes);
/* Can't find the requested resourse */
app.use((req, res, next) => {
const error = new Error("Resource not found");
error.status = 404;
next(error);
});
/* Any other error */
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message,
},
});
});
// Start server
app.listen(3000, () => console.log("Server has started on port 3000..."));