Created
September 24, 2018 18:52
-
-
Save rickycodes/64891f7a0c254ca185102a6190b0b9e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 express = require('express') | |
const app = express() | |
const port = 3000 // get this from config | |
const host = 'localhost' // get this from config | |
const startUpMsg = `proxy listening on port ${port}!` | |
const serveIndex = require('serve-index') | |
const Dat = require('dat-node') | |
const path = require('path') | |
// this dats folder could also be configurable | |
app.use(express.static(__dirname + '/dats')) | |
app.use('/', serveIndex(__dirname + '/dats')) | |
app.use(function (req, res, next) { | |
const orig = req.url | |
const { referer, host } = req.headers | |
if (referer && /^(\/)/.test(req.url)) { | |
const folder = referer.replace(`http://${host}/`, '') | |
res.redirect(`/${folder.replace('/', '')}${req.url}`) | |
} | |
next() | |
}) | |
app.get('/dat/:key', (req, res) => { | |
const { key } = req.params | |
Dat(`./dats/${key}`, { | |
key: key | |
}, function (err, dat) { | |
if (err) throw err | |
dat.joinNetwork({}, _ => { | |
res.json({ | |
url: `http://${host}:${port}/${key}` | |
}) | |
}) | |
}) | |
}) | |
app.listen(port, _ => console.log(startUpMsg)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment