Skip to content

Instantly share code, notes, and snippets.

@espeon
Created March 17, 2020 05:36
Show Gist options
  • Select an option

  • Save espeon/cae745cf5c83648116e28b7f414ae17c to your computer and use it in GitHub Desktop.

Select an option

Save espeon/cae745cf5c83648116e28b7f414ae17c to your computer and use it in GitHub Desktop.
const express = require("express"),
app = express(),
port = process.env.PORT || 5000;
const axios = require("axios");
const needle = require("needle");
app.get("/", (req, res) => {
res.json(
"hi"
);
});
app.get("/ipfs/:multihash", (req, res) => {
let multihash = req.params.multihash;
const creatornode = new Promise(function(resolve, reject) {
axios
.get("https://creatornode.audius.co/ipfs/" + multihash, {
timeout: 2000
})
.then(function(response) {
resolve("https://creatornode.audius.co/ipfs/" + multihash);
})
.catch(function(error) {
console.log(error);
});
});
const creatornode2 = new Promise(function(resolve, reject) {
axios
.get("https://creatornode2.audius.co/ipfs/" + multihash, {
timeout: 2000
})
.then(function(response) {
resolve("https://creatornode2.audius.co/ipfs/" + multihash);
})
.catch(function(error) {
console.log(error);
});
});
const ipfsio = new Promise(function(resolve, reject) {
axios
.get("https://ipfs.io/ipfs/" + multihash, {
timeout: 2000
})
.then(function(response) {
resolve("https://ipfs.io/ipfs/" + multihash);
})
.catch(function(error) {
console.log(error);
});
});
Promise.race([creatornode, creatornode2, ipfsio]).then(function(value) {
console.log("creatornode proxy request:", value)
needle.get(value)
.on('data', streamData => { res.write(streamData) })
.on('end', () => { res.end() })
});
});
app.use(function(req, res) {
res.status(404).json(`404`);
});
const listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment