Created
October 23, 2020 09:10
-
-
Save czarly/72784604ae15d09be5e777c85b00e6c3 to your computer and use it in GitHub Desktop.
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
const http = require("http"); | |
const url = require('url'); | |
// only http supported | |
const eth_node_url = "http://135.181.19.221:8545"; | |
const forward_url = url.parse(eth_node_url); | |
var server = http.createServer((req, res) => { | |
let input = []; | |
req.on("data", (chunk) => { | |
input.push(chunk); | |
}); | |
req.on("end", () => { | |
let body; | |
try { | |
body = JSON.parse(input); | |
console.log("parsed JSON request successfully") | |
body = JSON.stringify(body); | |
} catch (exception) { | |
console.log("can not parse JSON request", input.toString('utf8')); | |
} | |
const options = { | |
protocol: forward_url.protocol, | |
hostname: forward_url.hostname, | |
port: forward_url.port || 80, | |
path: forward_url.path, | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': body.length | |
} | |
}; | |
const req = http.request(options, (result) => { | |
let output = ''; | |
result.on('data', (chunk) => { | |
output += chunk; | |
}); | |
result.on('end', () => { | |
res.writeHead(200, { | |
'Content-Type': 'application/json' | |
}); | |
res.end(output.toString('utf8')); | |
//console.log('delivered output successfully', output.toString('utf8')); | |
}); | |
}).on("error", (err) => { | |
console.log("Error: ", err.message); | |
}); | |
req.write(body); | |
req.end(); | |
//console.log('forwarded input', body); | |
}); | |
}); | |
console.log("listening on port 7777"); | |
server.listen(7777); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment