Created
August 12, 2015 07:28
-
-
Save ishiduca/cf67b56c117adab3a926 to your computer and use it in GitHub Desktop.
example hogan-hammer
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 http = require('http') | |
var fs = require('fs') | |
var url = require('url') | |
var Hammer = require('hogan-hammer') | |
var anyBody = require('body/any') | |
var port = process.env.PORT || 3000 | |
var dashboard = path.join(__dirname, 'dashboard.html') | |
http.createServer(function (req, res) { | |
var hammer = Hammer().once('error', onError) | |
var writer = hammer.ws | |
writer.write(map(url.parse(req.url, true).query)) | |
if ('POST' === req.method.toUpperCase()) { | |
anyBody(req, function (err, raw) { | |
;(err) ? onError(err) : writer.end(map(raw)) | |
}) | |
} | |
else | |
writer.end() | |
fs.createReadStream(dashboard).once('error', onError).pipe(hammer).pipe(res) | |
function onError (err) { | |
console.error(err) | |
res.statusCode = err.statusCode || 500 | |
res.end(err.message) | |
} | |
}).listen(port, function () { | |
console.log('[server start to listen on port %s]', port) | |
console.log('[process.pid %s]', process.pid) | |
}) | |
function map (q) { | |
var list = q.list || [] | |
q.list = Array.isArray(list) ? list : [list] | |
return q | |
} |
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> | |
<h1>{{title}}</h1> | |
<ul> | |
{{#list}} | |
<li>{{.}}</li> | |
{{/list}} | |
</ul> | |
</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
{ | |
"dependencies": { | |
"body": "^5.1.0", | |
"hogan-hammer": "0.0.4" | |
}, | |
"scripts": { | |
"start": "PORT=8080 node app &" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment