-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (27 loc) · 947 Bytes
/
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
const express = require('express');
const proxy = require('http-proxy');
const http = require('http');
const path = require('path');
const app = express();
const proxyServer = proxy.createServer();
const isProduction = process.env.NODE_ENV === 'production';
const pathToStaticDir = path.resolve(__dirname, 'public');
app.use(express.static(pathToStaticDir));
if (!isProduction) {
const bundler = require('./config/bundler');
const devPort = 8080;
bundler(devPort);
app.all('/build/*', (req, res) => {
const target = `localhost:${devPort}`;
proxyServer.web(req, res, { target });
});
}
app.get('*', (req, res) => {
const pathToIndex = path.join(pathToStaticDir, 'index.html');
res.status(200).sendFile(pathToIndex);
});
const PORT = process.env.PORT || 3001;
proxyServer.on('error', err => {
console.log('Could not connect to proxy: ', err);
});
app.listen(PORT, () => console.log('App is listening on port', PORT));