Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Last active August 29, 2015 14:22
Show Gist options
  • Save ishiduca/dd8aacdaa7feec721c02 to your computer and use it in GitHub Desktop.
Save ishiduca/dd8aacdaa7feec721c02 to your computer and use it in GitHub Desktop.
example of "leveldb + trumpet"
'use strict'
var path = require('path')
var fs = require('fs')
var util = require('util')
var http = require('http')
var through = require('through2')
var trumpet = require('trumpet')
var level = require('level')
var tableHTML = path.join(__dirname, 'table.html')
var trHTML = path.join(__dirname, 'tr.html')
var formatTr = fs.readFileSync(trHTML, 'utf8')
var db = level(path.join(__dirname, './dbs/count'))
var s = http.createServer(function (req, res) {
res.setHeader('content-type', 'text/html; charset=utf-8')
var tr = trumpet()
db.createReadStream()
.pipe(map({start: 0}))
.pipe(tr.select('tbody').createWriteStream())
fs.createReadStream(tableHTML).on('error', onError)
.pipe(tr)
.pipe(res)
function onError (err) {
res.statusCode = 500
res.end(String(err))
}
})
s.listen(3000, console.log.bind(console, 'server start to listen on port "3000"'))
function map (opt) {
var count = opt.start || 0
return through.obj(function (pair, enc, done) {
var key = typeof pair.key === 'string' ? JSON.parse(pair.key) : pair.key
done(null, util.format(formatTr, count++, key.category, key.value, pair.value))
})
}
<!doctype html>
<body>
<table>
<thead>
<tr>
<th class="number">no.</th>
<th class="category">category</th>
<th class="value">value</th>
<th class="count">count</th>
</tr>
</thead>
<tbody></tbody>
</table>
</body>
<tr>
<td class="number">%d</td>
<td class="category">%s</td>
<td class="value">%s</td>
<td class="count">%d</td>
</tr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment