Skip to content

Instantly share code, notes, and snippets.

@xtools-at
Last active July 29, 2018 13:56
Show Gist options
  • Save xtools-at/1a1e8f99d7ee66925f19f4365b1e8ef6 to your computer and use it in GitHub Desktop.
Save xtools-at/1a1e8f99d7ee66925f19f4365b1e8ef6 to your computer and use it in GitHub Desktop.
MyEtherAPI Proxy in Express
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
// create our app
var app = express();
const PORT = process.env.PORT || 3000;
// body parser for json requests
app.use(bodyParser.json());
// Proxy for MyEtherAPI calls, as Google decided to classify the URL as malware
app.post('/api/rpc/eth', function (request, response, next){
var data = request.body;
if (data) {
axios.post('https://api.myetherapi.com/eth', data).then((res) => {
if (res.data) {
response.send(res.data); // success, forward API response
} else {
response.status(500).send({status: 500, error: true, message: "Empty response from RPC API"}); // internal error
}
}).catch(e => {
// rate limit hit, client needs to call API themself directly
response.redirect(302, 'https://api.myetherapi.com/eth');
});
} else {
response.status(400).send({status: 400, error: true, message: "No POST body"}); // bad request
}
});
app.get('/api/rpc/eth', function (request, response, next){
response.status(400).send({status: 400, error: true, message: "Only HTTP POST supported"}); // bad request
});
// start server
app.listen(PORT, function () {
console.log('MyEtherAPI proxy is up on port ' + PORT);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment