-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use native http module, stream files to the browser - adding liverelo…
…ad via through2
- Loading branch information
Showing
2 changed files
with
49 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters