-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (108 loc) · 3.08 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require('express');
const app = express();
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const multer = require('multer');
require('dotenv').config({ path: `${__dirname}/.env` });
const { renameIncomeImagePattern } = require('./global/helper');
const PORT = process.env.PORT || 8090;
const { getCategory } = require('./global/helper');
const constants = require('./global/constants');
const {
auth,
publicRoute,
work,
contact,
shots,
photos,
slider,
} = require('./routes');
const storageConfig = multer.diskStorage({
destination(req, file, callback) {
try {
const { domain } = req.headers; // 🔴
if (domain) {
const category = getCategory(req.originalUrl, constants.categories);
if (category) {
const dest = path.resolve(`${__dirname}/public/uploads/${domain}/${category}`);
fs.access(dest, (err) => {
if (err) {
console.error('Directory does not exist ✘ ⛔ ', dest);
return fs.mkdir(dest, (error2) => callback(error2, dest));
}
console.warn('Directory exists ✔ ✅');
return callback(null, dest);
});
} else {
// обробити помилку
console.error('FILE UPLOADING ERROR');
}
}
} catch (e) {
console.error(e);
}
// how save path to image in DB
// https://stackoverflow.com/questions/46975942/how-to-send-image-name-in-database-using-multer-and-express/47560629
},
filename(req, file, callback) {
const { domain } = req.headers; // 🔴
let filename = `${file.originalname}`.replace(renameIncomeImagePattern, ''); // remove special character from str
console.log('FILE', file);
if (!filename.length) {
filename = `image_${Date.now()}`;
}
filename = `${Date.now()}_${domain}_${filename}`;
callback(null, filename);
},
});
const corsConfig = {
// origin: 'http://localhost:8080',
// optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
};
// plugins:
app.use(cors(corsConfig));
app.use(multer({ storage: storageConfig }).any());
app.use(express.json({ limit: '1mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
// routes:
app.use((req, res, next) => {
res.header(
'Access-Control-Allow-Headers',
'x-access-token, Origin, Content-Type, Accept',
);
next();
});
const routeItems = [
{
path: '/public',
routes: [
publicRoute,
],
},
{
path: '/api/v1',
routes: [
auth,
work,
contact,
shots,
photos,
slider,
],
},
];
for (const item of routeItems) {
/* eslint-disable-next-line no-shadow */
const { path, routes } = item;
for (const route of routes) {
app.use(path, route);
}
}
// routes error handler
app.use((req, res) => {
res.status(404).send({ message: 'Not Found' });
});
// run server
const server = app.listen(PORT, () => console.log('\x1b[33m%s\x1b[0m', `Server started on http://localhost:${PORT}${routeItems[1].path}`));
server.setTimeout(5000);