Skip to content

Instantly share code, notes, and snippets.

@liddiard
Created September 8, 2015 19:27
Show Gist options
  • Save liddiard/4a03e9feecda595e11c6 to your computer and use it in GitHub Desktop.
Save liddiard/4a03e9feecda595e11c6 to your computer and use it in GitHub Desktop.
var http = require('http');
var fs = require('fs');
var argv = require('yargs').argv;
var cheerio = require('cheerio');
var DEFAULT_TEMPLATE = "http://uclabruins.com/ViewArticle.dbml?ATCLID=209624560&DB_OEM_ID=30500";
var template = argv.template || DEFAULT_TEMPLATE;
var pageFilename = argv._[0];
if (!pageFilename) {
console.error('ERROR: No page specified to preview.');
process.exit(1);
}
fs.readFile(pageFilename, function(err, page){
if (err) {
console.error(err);
process.exit(1);
}
getTemplate(page.toString());
});
function getTemplate(page) {
http.get(template, function(res){
res.on('data', function(chunk){
replaceArticle(chunk.toString(), page);
});
});
}
function replaceArticle(template, page) {
$ = cheerio.load(template);
$('#article-content').empty();
$('#article-content').append(page);
servePreview($.html());
}
function servePreview(html) {
http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
}).listen(3001, 'localhost');
}
@amoskyler
Copy link

var app =  http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(html);
  });

app.get(template, function(){});

app.listen(3001, 'localhost');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment