git ssb

1+

dinoworm ๐Ÿ› / catstack



Commit 5f435bb296e209bc2177fbe91c820ae8c38dc69e

fix proxy

- log errors
- delay requests with ECONNRESET error for one second
Michael Williams committed on 2/28/2016, 9:13:01 PM
Parent: 49f87e1c8d054830b90262388dfb432503a3905c

Files changed

app/proxy.jschanged
package.jsonchanged
proxy.jschanged
app/proxy.jsView
@@ -2,16 +2,35 @@
22 const httpProxy = require('http-proxy')
33 const Url = require('url')
44 const startsWith = require('lodash').startsWith
55 const assign = require('lodash').assign
6+const redirect = require('predirect')
67
78 module.exports = { createServer }
89
910 function createServer (config) {
1011 const proxy = httpProxy.createProxyServer({
1112 ignorePath: true
1213 })
1314
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+
1433 const server = http.createServer(function (req, res) {
1534 const url = Url.parse(req.url)
1635
1736 if (matches(config.api, url.pathname)) {
package.jsonView
@@ -21,9 +21,9 @@
2121 "dev:assets": "cpx \"app/assets/**/*\" build -w",
2222 "dev:livereload": "wtch -d build -e html,css,png,gif,jpg | garnish --level debug",
2323 "dev:api": "node-dev api",
2424 "dev:static": "node-dev static",
25- "dev:proxy": "node-dev proxy",
25+ "dev:proxy": "node-dev proxy | garnish --level debug",
2626 "prod:render-browser": "browserify app/render -o build/bundle.js -g envify -g uglifyify",
2727 "prod:render-node": "node render",
2828 "prod:assets": "cpx \"app/assets/**/*\" build",
2929 "prod:api": "node api",
proxy.jsView
@@ -4,6 +4,10 @@
44
55 const server = createServer(config)
66
77 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+ }))
913 })

Built with git-ssb-web