Skip to content

Instantly share code, notes, and snippets.

@rickycodes
Created September 24, 2018 18:52
Show Gist options
  • Save rickycodes/64891f7a0c254ca185102a6190b0b9e7 to your computer and use it in GitHub Desktop.
Save rickycodes/64891f7a0c254ca185102a6190b0b9e7 to your computer and use it in GitHub Desktop.
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