Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

es6ify server.js and include in eslint #2560

Merged
merged 2 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Getting Started (using local node)

- You need nodejs 4.2.2 and a running `weavescope` container
- You need at least Node.js 6.9.0 and a running `weavescope` container
- Setup: `npm install`
- Develop: `BACKEND_HOST=<dockerhost-ip> npm start` and then open `http://localhost:4042/`

Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"start-production": "NODE_ENV=production node server.js",
"test": "jest",
"coveralls": "cat coverage/lcov.info | coveralls",
"lint": "eslint app",
"lint": "eslint app server.js",
"clean": "rm build/app.js",
"noprobe": "../scope stop && ../scope launch --no-probe --app.window 8760h",
"loadreport": "npm run noprobe && sleep 1 && curl -X POST -H \"Content-Type: application/json\" http://$BACKEND_HOST/api/report -d"
Expand Down
81 changes: 38 additions & 43 deletions client/server.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
var express = require('express');
var http = require('http');
var httpProxy = require('http-proxy');
var HttpProxyRules = require('http-proxy-rules');
var url = require('url');
/* eslint no-console: 0 */
const express = require('express');
const http = require('http');
const httpProxy = require('http-proxy');
const HttpProxyRules = require('http-proxy-rules');

var app = express();
const app = express();

var BACKEND_HOST = process.env.BACKEND_HOST || 'localhost';
var WEBPACK_SERVER_HOST = process.env.WEBPACK_SERVER_HOST || 'localhost';
const BACKEND_HOST = process.env.BACKEND_HOST || 'localhost';

/************************************************************
/**
*
* Proxy requests to:
* - /api -> :4040/api
*
************************************************************/

var backendProxy = httpProxy.createProxy({
const backendProxy = httpProxy.createProxy({
ws: true,
target: 'http://' + BACKEND_HOST + ':4040'
});
backendProxy.on('error', function(err) {
console.error('Proxy error', err);
target: `http://${BACKEND_HOST}:4040`,
});
backendProxy.on('error', err => console.error('Proxy error', err));
app.all('/api*', backendProxy.web.bind(backendProxy));

/************************************************************
/**
*
* Production env serves precompiled content from build/
*
Expand All @@ -35,7 +32,7 @@ if (process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
}

/*************************************************************
/**
*
* Webpack Dev Middleware with Hot Reload
*
Expand All @@ -45,11 +42,11 @@ if (process.env.NODE_ENV === 'production') {
*************************************************************/

if (process.env.NODE_ENV !== 'production') {
var webpack = require('webpack');
var webpackMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var config = require('./webpack.local.config');
var compiler = webpack(config);
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.local.config');
const compiler = webpack(config);

app.use(webpackMiddleware(compiler, {
// required
Expand All @@ -63,55 +60,53 @@ if (process.env.NODE_ENV !== 'production') {
}


/******************
/**
*
* Express server
*
*****************/

var port = process.env.PORT || 4042;
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
const port = process.env.PORT || 4042;
const server = app.listen(port, 'localhost', () => {
const host = server.address().address;

console.log('Scope UI listening at http://%s:%s', host, port);
});

server.on('upgrade', backendProxy.ws.bind(backendProxy));


/*************************************************************
/**
*
* Path proxy server
*
*************************************************************/

var proxyRules = new HttpProxyRules({
const proxyRules = new HttpProxyRules({
rules: {
'/scoped/': 'http://localhost:' + port
'/scoped/': `http://localhost:${port}`,
}
});

var pathProxy = httpProxy.createProxy({ws: true});
pathProxy.on('error', function(err) { console.error('path proxy error', err); });
var pathProxyPort = port + 1;
const proxyPathServer = http.createServer(function(req, res) {
var target = proxyRules.match(req);
const pathProxy = httpProxy.createProxy({ws: true});
pathProxy.on('error', err => console.error('path proxy error', err));
const pathProxyPort = port + 1;
const proxyPathServer = http.createServer((req, res) => {
const target = proxyRules.match(req);
if (!target) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('No rules matched! Check out /scoped/');
return;
return res.end('No rules matched! Check out /scoped/');
}
return pathProxy.web(req, res, {target: target});
}).listen(pathProxyPort, function() {
var pathProxyHost = proxyPathServer.address().address;
return pathProxy.web(req, res, {target});
}).listen(pathProxyPort, 'localhost', () => {
const pathProxyHost = proxyPathServer.address().address;
console.log('Scope Proxy Path UI listening at http://%s:%s/scoped/',
pathProxyHost, pathProxyPort);
pathProxyHost, pathProxyPort);
});

proxyPathServer.on('upgrade', function(req, socket, head) {
var target = proxyRules.match(req);
proxyPathServer.on('upgrade', (req, socket, head) => {
const target = proxyRules.match(req);
if (target) {
return pathProxy.ws(req, socket, head, {target: target});
pathProxy.ws(req, socket, head, {target});
}
});