-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
63 lines (51 loc) · 1.56 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
const express = require('express');
const logger = require('morgan');
const cors = require('cors');
const compression = require('compression');
const sslRedirect = require('heroku-ssl-redirect');
const app = express();
const env = app.get('env');
console.log('env:', env);
/**
* Custom .env cofiguration
*/
if (env !== 'production') {
const path = require('path');
const config = {};
if (env === 'test') {
config.path = path.join(__dirname, '/config/test.env');
} else {
config.path = path.join(__dirname, '/config/dev.env');
}
require('dotenv').config(config);
}
if (env !== 'test') {
require('./data/index'); // connecting to database
}
const imagesRoute = require('./routes/images.js');
const authRouter = require('./routes/auth.js');
const usersRouter = require('./routes/users.js');
const cloudinaryRouter = require('./routes/cloudinary.js');
// enable ssl redirect in production
app.use(sslRedirect());
app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(compression());
app.use('/api/images/', imagesRoute);
app.use('/api/auth/', authRouter);
app.use('/api/users/', usersRouter);
app.use('/api/cloudinary/', cloudinaryRouter);
if (env === 'production') {
const path = require('path');
app.use(express.static(path.join(__dirname, '/client/build')));
app.get('*', (req, res) =>
res.sendFile(path.join(__dirname, '/client/build/index.html'))
);
}
if (env !== 'test') {
const PORT = process.env.PORT || '5000';
app.listen(PORT, () => console.log(`server started on port ${PORT}`));
} else {
module.exports = app;
}