-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
50 lines (44 loc) · 1.54 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
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const helmet = require('helmet');
const compression = require('compression');
const usersApi = require('./routes/usersApi');
const goodsApi = require('./routes/goodsApi');
const reviewsApi = require('./routes/reviewsApi');
const genresApi = require('./routes/genresApi');
const mongoose = require('mongoose');
const config = require('./config/config');
const path = require('path');
const app = express();
const port = process.env.NODE_ENV === 'production' ? 81 : config.PORT;
app.use(cors());
app.use(helmet());
app.use(compression());
app.use(morgan(process.env.NODE_ENV === 'production' ? 'combined' : 'dev'));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.use('/static', express.static(path.join(__dirname, 'static')));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use('/api', usersApi, goodsApi, reviewsApi, genresApi);
if (process.env.NODE_ENV === 'production') {
app.use('/', express.static(path.join(__dirname, 'client', 'build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
(async () => {
try {
await mongoose.connect(config.MONGOURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
app.listen(port, () => {
console.log(`Запуск сервера на порту ${port}...`);
});
} catch (err) {
throw err;
}
})();