forked from usegravityapp/free-saas-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (29 loc) · 922 Bytes
/
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
require('dotenv').config();
const express = require('express');
const api = require('./api');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
// config express
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// api
app.use(api);
// serve static react files in production
if (process.env.NODE_ENV === 'production'){
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
});
}
// handle errors from controllers
app.use(function(err, req, res, next){
console.error(err);
const message = err.raw?.message || err.message || err.sqlMessage;
return res.status(500).send({ message: message });
});
// start server
const server = app.listen(port, async () => {
console.log('Welcome to Gravity 🚀');
});
module.exports = server;