Commit 5f435bb296e209bc2177fbe91c820ae8c38dc69e
fix proxy
- log errors - delay requests with ECONNRESET error for one secondMichael Williams committed on 2/28/2016, 9:13:01 PM
Parent: 49f87e1c8d054830b90262388dfb432503a3905c
Files changed
app/proxy.js | changed |
package.json | changed |
proxy.js | changed |
app/proxy.js | ||
---|---|---|
@@ -2,16 +2,35 @@ | ||
2 | 2 | const httpProxy = require('http-proxy') |
3 | 3 | const Url = require('url') |
4 | 4 | const startsWith = require('lodash').startsWith |
5 | 5 | const assign = require('lodash').assign |
6 | +const redirect = require('predirect') | |
6 | 7 | |
7 | 8 | module.exports = { createServer } |
8 | 9 | |
9 | 10 | function createServer (config) { |
10 | 11 | const proxy = httpProxy.createProxyServer({ |
11 | 12 | ignorePath: true |
12 | 13 | }) |
13 | 14 | |
15 | + proxy.on('error', function (err, req, res) { | |
16 | + console.log(JSON.stringify({ | |
17 | + name: 'proxy', | |
18 | + level: 'warn', | |
19 | + url: req.url, | |
20 | + message: err.message | |
21 | + })) | |
22 | + if (err.code === 'ECONNREFUSED') { | |
23 | + // HACK delay request for one second | |
24 | + setTimeout(function () { | |
25 | + redirect(req, res, `http://localhost:${config.proxy.port}${req.url}`) | |
26 | + }, 1000) | |
27 | + } else { | |
28 | + res.writeHead(500, { 'Content-Type': 'text/plain' }) | |
29 | + res.end('Internal Server Error') | |
30 | + } | |
31 | + }) | |
32 | + | |
14 | 33 | const server = http.createServer(function (req, res) { |
15 | 34 | const url = Url.parse(req.url) |
16 | 35 | |
17 | 36 | if (matches(config.api, url.pathname)) { |
package.json | ||
---|---|---|
@@ -21,9 +21,9 @@ | ||
21 | 21 | "dev:assets": "cpx \"app/assets/**/*\" build -w", |
22 | 22 | "dev:livereload": "wtch -d build -e html,css,png,gif,jpg | garnish --level debug", |
23 | 23 | "dev:api": "node-dev api", |
24 | 24 | "dev:static": "node-dev static", |
25 | - "dev:proxy": "node-dev proxy", | |
25 | + "dev:proxy": "node-dev proxy | garnish --level debug", | |
26 | 26 | "prod:render-browser": "browserify app/render -o build/bundle.js -g envify -g uglifyify", |
27 | 27 | "prod:render-node": "node render", |
28 | 28 | "prod:assets": "cpx \"app/assets/**/*\" build", |
29 | 29 | "prod:api": "node api", |
proxy.js | ||
---|---|---|
@@ -4,6 +4,10 @@ | ||
4 | 4 | |
5 | 5 | const server = createServer(config) |
6 | 6 | |
7 | 7 | server.listen(config.proxy.port, function () { |
8 | - console.log(`proxy server listening on port ${config.proxy.port}`) | |
8 | + console.log(JSON.stringify({ | |
9 | + name: 'proxy', | |
10 | + level: 'info', | |
11 | + message: `proxy server listening on port ${config.proxy.port}` | |
12 | + })) | |
9 | 13 | }) |
Built with git-ssb-web