-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwww
39 lines (34 loc) · 1.11 KB
/
www
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
const express = require('express');
const createError = require('http-errors');
const fs = require('fs');
const path = require('path');
const app = express();
const port = process.env.PORT || 5000;
app.listen(port);
console.log('server started ' + port);
const clientDirectory = path.join(__dirname, '/dist/tutorial/');
// Forward All Routes By Default to React Build if In Production Mode
if (fs.existsSync(clientDirectory) && process.env.NODE_ENV !== 'test') {
console.log('Production Or Development Environment');
app.use(express.static(clientDirectory));
app.get('/*', (req, res) => {
res.sendFile(path.join(clientDirectory, 'index.html'));
});
}
// catch 404 and forward to error handler
app.use(function(req, res, next) {
try {
next(createError(404));
} catch (err) {
next(err);
}
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// return the error message
res.status(err.status || 500);
res.json({ error: err });
});