server.js
2.21 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright © 2016 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable import/no-commonjs */
/* eslint-disable global-require */
/* eslint-disable import/no-nodejs-modules */
const path = require('path');
const webpack = require('webpack');
const historyApiFallback = require("connect-history-api-fallback");
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config');
const express = require('express');
const http = require('http');
const httpProxy = require('http-proxy');
const forwardHost = 'localhost';
const forwardPort = 8080;
const app = express();
const server = http.createServer(app);
const PORT = 3000;
const compiler = webpack(config);
app.use(historyApiFallback());
app.use(webpackDevMiddleware(compiler, {noInfo: true, publicPath: config.output.publicPath}));
app.use(webpackHotMiddleware(compiler));
const root = path.join(__dirname, '/src');
app.use('/static', express.static(root));
const apiProxy = httpProxy.createProxyServer({
target: {
host: forwardHost,
port: forwardPort
}
});
console.info(`Forwarding API requests to http://${forwardHost}:${forwardPort}`);
app.all('/api/*', (req, res) => {
apiProxy.web(req, res);
});
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'src/index.html'));
});
server.on('upgrade', (req, socket, head) => {
apiProxy.ws(req, socket, head);
});
server.listen(PORT, '0.0.0.0', (error) => {
if (error) {
console.error(error);
} else {
console.info(`==> 🌎 Listening on port ${PORT}. Open up http://localhost:${PORT}/ in your browser.`);
}
});