-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
93 lines (81 loc) · 2.48 KB
/
index.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
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const session = require("express-session");
const mongoose = require("mongoose");
const cors = require("cors");
const mountRoutes = require("./routes");
const multer = require("multer");
const crypto = require("crypto");
const GridFsStorage = require("multer-gridfs-storage");
const imageRouter = require("./routes/image");
const RequestScheduler = require("./scheduler/request.scheduler");
const BeaconScheduler = require("./scheduler/beacon.scheduler");
const app = express();
const port = process.env.PORT || 5000;
if (process.env.PORT) {
app.use((req, res, next) => {
if (req.header("x-forwarded-proto") !== "https")
res.redirect(`https://${req.header("host")}${req.url}`);
else next();
});
}
app.use(cors());
app.use(require("morgan")("dev"));
app.use(express.static(path.join(__dirname, "public")));
app.use(
session({
secret: "passport-tutorial",
cookie: { maxAge: 60000 },
resave: false,
saveUninitialized: false,
})
);
// Set up mongoose connection
let dev_db_url =
"mongodb+srv://debanik:[email protected]/RequestRedesignDB?retryWrites=true&w=majority";
let mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
mongoose.Promise = global.Promise;
let db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
// create storage engine
const storage = new GridFsStorage({
url: mongoDB,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString("hex") + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: "uploads",
};
resolve(fileInfo);
});
});
},
});
const upload = multer({ storage });
require("./models/association.model");
require("./models/user.model");
require("./config/passport");
// RequestScheduler.request_scheduler();
// BeaconScheduler.BeaconScheduler();
app.use(bodyParser.json({ limit: "50mb" }));
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 50000,
})
);
app.use("/api/image", imageRouter(upload));
mountRoutes(app);
app.listen(port, () => console.log(`Listening on port ${port}`));