-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (79 loc) · 2.34 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
94
95
96
97
98
99
100
101
const express = require("express")
const app = express()
const mongoose = require("mongoose")
const dotenv = require("dotenv")
const helmet = require("helmet")
const morgan = require("morgan")
const cors = require("cors")
const multer = require("multer")
const path = require("path")
const aws = require('aws-sdk');
const multerS3 = require('multer-s3');
const bodyParser = require('body-parser')
dotenv.config();
app.use(cors({
origin: '/'
}));
aws.config.update({
secretAccessKey: process.env.SECRETACCESSKEY,
accessKeyId: process.env.ACCESSKEYID,
region: process.env.REGION
});
s3 = new aws.S3();
const userRoute = require("./routes/users")
const authRoute = require("./routes/auth")
const postRoute = require("./routes/posts")
const port = process.env.PORT || 8800
mongoose.connect(process.env.MONGO_URL, () =>{
console.log("connected to MongoDB")
});
// if you use the /images path, don't make any request, instead go to the directory below
// app.use("/images", express.static(path.join(__dirname, "public/images")))
app.use(express.static(path.join(__dirname, "client", "build")))
//middleware
app.use(express.json());
app.use(helmet());
app.use(
helmet.contentSecurityPolicy({
directives: {
...helmet.contentSecurityPolicy.getDefaultDirectives(),
"img-src": ["'self'", "kizbook-imgs.s3.eu-west-1.amazonaws.com"],
},
})
);
app.use(bodyParser.json());
app.use(morgan("common"));
const upload = multer({
storage: multerS3({
s3: s3,
bucket: 'kizbook-imgs',
key: function (req, file, cb) {
console.log(file),
cb(null, req.body.name); //use Date.now() for unique file keys
},
})
});;
app.post("/api/upload", upload.array("file"), (req, res)=>{
try{
return res.status(200).json("File uploaded successfully.")
}catch(err){
console.log(err)
}
})
app.use("/api/users", userRoute);
app.use("/api/auth", authRoute);
app.use("/api/posts", postRoute);
// Serve static assets if in production
if(process.env.NODE_ENV === "production") {
// set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
app.listen(port, ()=>{
console.log("Backend server is running.")
})