Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 118 lines (105 sloc) 2.99 KB
#!/usr/bin/env node
var server = require('http').createServer(httpHandler),
spawn = require('child_process').spawn,
exec = require('child_process').exec,
io = require('socket.io').listen(server),
fs = require('fs'),
path = require('path'),
server,
socket;
server.listen(8090);
function httpHandler(req, res) {
switch(req.method)
{
case 'GET':
var filePath = __dirname + req.url;
if (filePath == __dirname + '/'){
filePath = __dirname + '/index.html';
}
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
}
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
res.writeHead(500);
res.end();
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
}
else {
res.writeHead(404);
res.end();
}
});
break;
// case 'HEAD':
// res.writeHead(200);
// res.end();
// exec('open -g http://localhost:8090', function(error, stdout, stderr){
// http.request({port: 8090})
// });
// break;
case 'DELETE':
socket.emit('die');
process.exit();
break;
case 'PUT':
var newHTML = '';
var gfm = spawn(__dirname + '/node_modules/docter/bin/github-flavored-markdown.rb', ['--unstyled']);
req.on('data', function(chunk){
gfm.stdin.write(chunk);
});
req.on('end', function(){
gfm.stdin.end();
res.writeHead(200);
res.end();
});
gfm.stdout.on('data', function(data) {
newHTML += data;
});
gfm.on('exit',function(ecode){
socket.emit('newContent', newHTML);
});
break;
default:
}
}
io.set('log level', 1);
io.sockets.on('connection', function(sock){
socket = sock;
process.stdout.write('connection established!');
var newHTML = '';
var gfm = spawn(__dirname + '/node_modules/docter/bin/github-flavored-markdown.rb', ['--unstyled']);
process.stdin.on('data', function(chunk){
gfm.stdin.write(chunk);
});
process.stdin.on('end', function(){
gfm.stdin.end();
});
gfm.stdout.on('data', function(data) {
newHTML += data;
});
gfm.on('exit',function(ecode){
socket.emit('newContent', newHTML);
});
process.stdin.resume();
});
if (process.platform.toLowerCase().indexOf('darwin') >= 0){
exec('open -g http://localhost:8090', function(error, stdout, stderr){});
}
else { // assume unix/linux
exec('xdg-open http://localhost:8090', function(error, stdout, stderr){});
}