Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var leveled = require('leveled')
var fs = require('fs')
var http = require('http')
var db
// insert values
if (!fs.existsSync('db')) {
db = leveled('db')
var batch = db.batch()
for (var i = 0, t = Date.now(); i < 120000; i++, t++) {
batch.put(t, '1337,1337,1337,1337,1337,1337')
}
batch.writeSync()
}
if (!db) db = leveled('db')
http.createServer(function (req, res) {
if (req.url == '/favicon.ico') return res.end()
var start = Date.now()
if (req.url == '/') {
db.find('*', function (err, entries) {
if (err) return res.end(err.toString())
print(res, entries)
})
} else if (req.url.indexOf(',') > -1) {
var url = req.url.substr(1, req.url.length - 1)
var segs = url.split(',')
db.range(segs[0], segs[1], function (err, entries) {
if (err) return res.end(err.toString())
print(res, entries)
})
} else {
var url = req.url.substr(1, req.url.length - 1)
db.get(url, function (err, val) {
if (err) return res.end(err.toString())
res.end(url + ' : ' + val)
})
}
}).listen(3000)
/**
* REPLICATE HERE
*/
/**
* Utility
*/
function print (res, entries) {
var buf = []
Object.keys(entries).forEach(function (key) {
buf.push(key + ' : ' + entries[key])
})
res.end(buf.join('\n'))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.