Last active
December 17, 2018 13:25
-
-
Save headquarters/850cbb199ff397c6da56fb8d86113a7e to your computer and use it in GitHub Desktop.
Socket hang up: server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var HttpsProxyAgent = require('https-proxy-agent'); | |
require('isomorphic-fetch'); | |
// Attempt to fetch the origin server, but proxied through the proxy server...works in browser, but not on the CLI | |
// with `node fetch.js` | |
fetch('http://localhost:8818', { | |
agent: new HttpsProxyAgent('http://username:password@localhost:9818') | |
}).then((res) => { | |
console.log('Fetch response', res); | |
}).catch((err) => { | |
console.error('Fetch error', err); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ DEBUG=* node fetch.js | |
https-proxy-agent creating new HttpsProxyAgent instance: Url { protocol: 'http:', slashes: true, auth: 'username:password', host: 'localhost:9818', port: '9818', hostname: 'localhost', hash: null, search: null, query: null, pathname: '/', path: '/', href: 'http://username:password@localhost:9818/' } +0ms | |
isomorphic-fetch: calling node-fetch | |
node-fetch: sending request | |
https-proxy-agent onend +24ms | |
https-proxy-agent onclose had error false +1ms | |
node-fetch: error { Error: socket hang up | |
at createHangUpError (_http_client.js:322:15) | |
at Socket.socketCloseListener (_http_client.js:363:25) | |
at Socket.emit (events.js:187:15) | |
at TCP._handle.close (net.js:606:12) code: 'ECONNRESET' } | |
Fetch error { FetchError: request to http://localhost:8818 failed, reason: socket hang up | |
at ClientRequest.<anonymous> (/path/to/node_modules/node-fetch/index.js:136:11) | |
at ClientRequest.emit (events.js:182:13) | |
at Socket.socketCloseListener (_http_client.js:363:11) | |
at Socket.emit (events.js:187:15) | |
at TCP._handle.close (net.js:606:12) | |
name: 'FetchError', | |
message: | |
'request to http://localhost:8818 failed, reason: socket hang up', | |
type: 'system', | |
errno: 'ECONNRESET', | |
code: 'ECONNRESET' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var httpProxy = require('http-proxy'); | |
// Create origin server at localhost:8818 | |
server = http.createServer(async (req, res) => { | |
console.log('Server received', req.url); | |
res.writeHead(200, { 'Content-Type': 'application/json' }); | |
res.write(JSON.stringify({ 'a': 'b' })); | |
res.end(); | |
}); | |
server.on('error', (err) => { | |
res.writeHead(500, { | |
'Content-Type': 'text/plain' | |
}); | |
res.end('Something went wrong.'); | |
}); | |
server.listen(8818, () => { console.log('Origin server is listening...'); }); | |
// Create proxy server at localhost:9818 pointing to localhost:8818 | |
proxy = httpProxy.createProxyServer({ target: 'http://localhost:8818'}).listen(9818, () => { | |
console.log('Proxy server is listening...'); | |
}); | |
proxy.on('error', function (err, req, res) { | |
res.writeHead(500, { | |
'Content-Type': 'text/plain' | |
}); | |
res.end('Something went wrong.'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment