-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (74 loc) · 2.52 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
let express = require("express");
const cors = require("cors");
const chalk = require('chalk')
const bodyParser = require("body-parser");
const dotenv = require('dotenv');
let multer = require("multer");
let path = require('path')
//<editor-fold desc="Server Set Up">
const app = express();
dotenv.config();
let allowedOrigins = [
"http://localhost:3000",
"http://localhost:3002",
"http://localhost:3001",
];
app.use(
cors({
origin: function (origin, callback) {
// allow requests with no origin
// (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
let msg =
"The CORS policy for this site does not " +
"allow access from the specified Origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
}
})
);
app.use(function (req, res, next) {
let origin = req.headers.origin;
if (allowedOrigins.includes(origin)) {
res.header("Access-Control-Allow-Origin", origin); // restrict it to the required domain
}
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
// app.use(bodyParser.json());
app.use(bodyParser.json({limit: '50mb'}));
// don't forget to put process.env.port for heroku
app.listen(process.env.PORT || 5001, function () {
console.log("App listening on", 5001);
});
const UPLOAD_FILES_DIR = "./uploads";
const storage = multer.diskStorage({
destination(req, file, cb) {
cb(null, UPLOAD_FILES_DIR);
},
filename(req, file = {}, cb) {
console.log(file, "filename")
const {originalname} = file;
let questionUid = originalname.split('.')[0]
const fileExtension = (originalname.match(/\.+[\S]+$/) || [])[0];
// cb(null, `${questionUid}${'SEPARATOR'}${Date.now()}${fileExtension}`);
}
});
const upload = multer({storage: storage});
//</editor-fold>
const cacheTime = 3600
// serve the build folder that is uploaded locally (you can host your js bundle on S3 or other hosting service if you want to)
app.use(express.static(path.join(__dirname + '/build'), {maxAge: cacheTime}));
// Atm serves the homepage
app.get('/', function (req, res) {
res.sendFile(path.resolve('build/index.html'));
})
// Catch all requests
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname + '/build/index.html'));
})