-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (67 loc) · 2.28 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
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const { v4: uuid } = require('uuid');
const multer = require('multer');
require('dotenv').config();
const shopRoutes = require('./routes/shop');
const app = express();
const MONGODB_URI =
`mongodb+srv://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@cluster0.n3pys.mongodb.net/rahimStore?retryWrites=true&w=majority`;
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, 'uploads', 'images'));
},
filename: (req, file, cb) => {
// const imageExtension = file.originalname.split(".").reverse()[0]; parse extName without help of "path" module.
const imageExtension = path.extname(file.originalname);
const photoName = `${uuid()}${imageExtension}`;
cb(null, photoName);
},
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg"
) {
cb(null, true);
} else {
cb(null, false);
}
};
// For fix to CORS errors
app.use((_, res, next) => {
const aca = 'Access-Control-Allow-';
res.setHeader(`${aca}Origin`, '*');
res.setHeader(`${aca}Methods`, 'GET, POST, PUT, DELETE');
res.setHeader(`${aca}Headers`, 'Content-Type, Authorization');
next();
});
app.use(bodyParser.json());
app.use(multer({ storage: fileStorage, fileFilter: fileFilter }).single('image'))
app.use('/images', express.static(path.join(__dirname, 'uploads', 'images')))
app.use('/shop', shopRoutes);
app.use((error, req, res, next) => {
console.log(error)
const status = error.statusCode || 500;
const message = error.message;
res.status(status).json({
message: message
});
})
mongoose.set("useNewUrlParser", true);
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);
mongoose.set("useUnifiedTopology", true);
mongoose
.connect(MONGODB_URI)
.then((_) => {
console.log('connect successfully>>>')
const port = process.env.PORT || 8080
app.listen(port);
})
.catch((err) => {
console.log(Error(`mongooseConnect ${err}`));
});