Skip to content

Instantly share code, notes, and snippets.

@mk30
Created January 1, 2014 19:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mk30/8210642 to your computer and use it in GitHub Desktop.
Save mk30/8210642 to your computer and use it in GitHub Desktop.
simple blog server
var http = require('http');
var fs = require('fs');
var hyperstream = require('hyperstream');
var article = require('markdown-directory')(__dirname + '/articles');
var server = http.createServer(function (req, res) {
if(req.url == "/"){
fs.readdir(__dirname + '/articles', function (err, files) {
res.end(files.map(function (file) {
var title = file.replace(/\.markdown$/, '');
return '<div><a href="/articles/' + title + '">'
+ title + '</a></div>'
;
}).join('\n'));
})
}
else{
var m = RegExp('^/articles/(.+)').exec(req.url);
if (!m) return res.end('cats');
res.setHeader('content-type', 'text/html');
fs.createReadStream(__dirname + '/article.html')
.pipe(hyperstream({
'title': m[1],
'#article': article(m[1]),
}))
.pipe(res)
;
}
});
server.listen(process.argv[2] || 80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment