Skip to content

Commit

Permalink
use native http module, stream files to the browser - adding liverelo…
Browse files Browse the repository at this point in the history
…ad via through2
  • Loading branch information
w33ble committed Apr 20, 2014
1 parent c0c70cb commit 35018b7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
93 changes: 46 additions & 47 deletions lib/serve/createServer.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,65 @@
var fs = require('fs');
var path = require('path');
var express = require('express');
var pf = require('portfinder');
var cheerio = require('cheerio');
var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');
var http = require('http');
var pf = require('portfinder');
var mime = require('mime');
var through2 = require('through2');
var cheerio = require('cheerio');
var exec = require('child_process').exec;

module.exports = function (port, lrPort, open, next) {

var app = express();
var server = http.createServer(function(req, res) {
var filePath = path.join(process.cwd(), req.url);
var mimetype = mime.lookup(filePath);
var fileStream = fs.createReadStream(filePath);

// livereload.js
// send correct mimetype
res.writeHead(200, {'Content-type': mimetype});

app.get('/livereload.js', function(req, res) {

res.sendfile(__dirname + '/public/livereload.js');

});

// inject livereload script tag in htm|html

app.get('/*.(htm|html)', function(req, res, next) {

// fetch passed in file path
var filename = req.params.join('.');
try {
var html = fs.readFileSync('./'+filename, 'utf8');
} catch(e) {
if (e.code === 'ENOENT') {
return next();
// handle errors
fileStream.on('error', function(err) {
if (err.code === 'ENOENT') {
return send404(res);
}
throw e;
throw err;
});

// if html, inject lr
if (filePath.match(/htm(l)?$/)) {
console.log('html', filePath);
fileStream
.pipe(through2.obj(function(file, enc, next) {
var script = "<script>document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':"+lrPort+"/livereload.js?snipver=1\"></' + 'script>')</script>";
var html = file.toString('utf8');
var $ = cheerio.load(html);
$('body').append(script);

this.push($.html());
next();
}))
.pipe(res);
} else {
// stream file to response
fileStream.pipe(res);
}

var $ = cheerio.load(html);
var script = "<script>document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':"+lrPort+"/livereload.js?snipver=1\"></' + 'script>')</script>";

$('body').append(script);

res.send($.html());

});

// handle requests without filename

app.get('/', function(req, res) {
res.redirect('index.html');
});

// find available http port

pf.getPort({port: port}, function(err, httpPort){

app
.use(express.static(path.resolve('./')))
.listen(httpPort, function() {
server.listen(httpPort, function() {
console.log('Listening on', httpPort);
if (open) {
exec('open http://localhost:'+httpPort);
}
next(null, app);
});
next(null, server);
});

});
};

};
function send404(res) {
res.writeHead(404, {'Content-type': 'text/plain'});
res.end('Not Found');
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
"dependencies": {
"tiny-lr": "0.0.5",
"gulp": "~3.2.0",
"express": "~3.4.7",
"gulp-util": "~2.2.0",
"portfinder": "~0.2.1",
"cheerio": "~0.13.1",
"minimist": "0.0.8"
"minimist": "0.0.8",
"mime": "~1.2.11",
"through2": "~0.4.1"
},
"devDependencies": {
"mocha": "*",
Expand Down

0 comments on commit 35018b7

Please sign in to comment.