Skip to content

Instantly share code, notes, and snippets.

@jdkanani
Forked from ziahamza/server.js
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdkanani/b5843f661792456aba20 to your computer and use it in GitHub Desktop.
Save jdkanani/b5843f661792456aba20 to your computer and use it in GitHub Desktop.
var http = require('http'),
fs = require('fs'),
path = require('path'),
exec = require('child_process').exec;
function pipeDoc(inputPath, finalType, stream) {
var finalPath = path.dirname(inputPath)
+ "/" + path.basename(inputPath).split('.')[0] + ".html";
var convCommand = 'unoconv -f ' + finalType + " " + inputPath;
exec(convCommand,function(err, stdout, stderr) {
process.stdout.write(stdout);
fs.createReadStream(finalPath).pipe(stream);
});
}
http.createServer(function(rq, rs) {
rs.writeHead(200, {'Content-Type': 'text/html'});
var rqPath = rq.url;
if (path.extname(rqPath).length > 1) {
pipeDoc(rq.url, "html", rs);
}
else {
rs.end(
"<html><body><h1>Use the url http://localhost:8080/path_to_document to view in browser</h1></body></html>"
);
}
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment