diff --git a/lib/serve/createServer.js b/lib/serve/createServer.js index 24c8ef1..377fa62 100644 --- a/lib/serve/createServer.js +++ b/lib/serve/createServer.js @@ -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 = ""; + 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 = ""; - - $('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); + }); }); +}; -}; \ No newline at end of file +function send404(res) { + res.writeHead(404, {'Content-type': 'text/plain'}); + res.end('Not Found'); +}; diff --git a/package.json b/package.json index 4e0ccd2..a701cd7 100644 --- a/package.json +++ b/package.json @@ -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": "*",