-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
111 lines (97 loc) · 2.96 KB
/
app.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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const path = require("path");
const keys = require("./config/keys");
// import multer for storage
const multer = require("multer");
// import routes
const appRoute = require("./routes/api/appRoute");
const fetchDetails = require("./routes/api/fetchDetails");
const users = require("./routes/api/users");
const fetchUploads = require('./routes/api/fetchUploads')
const deleteRecords = require('./routes/api/deleteRecords')
const downloadRecords = require("./routes/api/downloadRecords")
// Initialise express app
const app = express();
// Bring in cors
const cors = require("cors");
// Bring in passport
const passport = require("passport");
// Adding passport middleware
app.use(passport.initialize());
// Configuration for passport
require("./config/passport")(passport);
// Add body-parser middleware to get body details
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(bodyParser.json());
// Use cors
app.use(cors());
app.use((request, response, next) => {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
response.header(
"Access-Control-Allow-Headers",
"Content-type, Accept, x-access-token, X-Key"
);
response.header("Access-Control-Allow-Credentials", true);
credentials: "same-origin";
if (request.method == "OPTIONS") {
response.status(200).end();
} else {
next();
}
});
//Multer storage directory
const storageDirectory = path.join(__dirname, "..", "storage");
// Multer - File Storage configuration
// The disk storage engine gives you full control on storing files to disk.
// For info https://github.com/expressjs/multer#diskstorage
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, storageDirectory);
},
filename: (req, file, cb) => {
// returns the extension of a file path.
// for more info: https://github.com/expressjs/multer#api
cb(null, Date.now() + path.extname(file.originalname));
}
});
// add the destination to multer
const upload = multer({ storageDirectory: storageDirectory });
// storing names to the values
app.set("storageSirectory", storageDirectory);
app.set("upload", upload);
// Use routing
app.use("/api/appRoute", appRoute);
app.use("/api/shared", fetchDetails);
app.use("/api/users", users);
app.use("/api/fetchUploads", fetchUploads)
app.use("/api/deleteRecords", deleteRecords)
app.use("/api/downloadRecords", downloadRecords)
// Add database connection
const db = keys.mongoURI;
mongoose
.connect(
db,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
)
.then(res => {
console.log("Database connected successfully!");
})
.catch(error => {
console.log(error);
});
// Add port
const PORT = process.env.PORT || 5010;
app.listen(PORT, () => {
console.log(`App is listening on port ${PORT}`);
});
module.exports = app;