-
Notifications
You must be signed in to change notification settings - Fork 218
/
server.js
46 lines (39 loc) · 1.21 KB
/
server.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
const path = require('path');
const express = require('express');
const cloudinary = require('cloudinary');
const app = require('./backend/app');
const connectDatabase = require('./backend/config/database');
const PORT = process.env.PORT || 4000;
// UncaughtException Error
process.on('uncaughtException', (err) => {
console.log(`Error: ${err.message}`);
process.exit(1);
});
connectDatabase();
cloudinary.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
// deployment
__dirname = path.resolve();
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '/frontend/build')))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'))
});
} else {
app.get('/', (req, res) => {
res.send('Server is Running! 🚀');
});
}
const server = app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`)
});
// Unhandled Promise Rejection
process.on('unhandledRejection', (err) => {
console.log(`Error: ${err.message}`);
server.close(() => {
process.exit(1);
});
});