Last active
August 29, 2015 14:22
-
-
Save ishiduca/dd8aacdaa7feec721c02 to your computer and use it in GitHub Desktop.
example of "leveldb + trumpet"
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
'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)) | |
}) | |
} |
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
<!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> |
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
<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