From b046159bd478319ad3c44f68c0d6e55dae0ba3ce Mon Sep 17 00:00:00 2001 From: Contra Date: Sat, 12 Jul 2014 14:31:18 -0700 Subject: [PATCH] fixes --- lib/serve/createServer.js | 63 +++++++++++++++++++-------------------- package.json | 22 +++++--------- test/main.coffee | 10 ------- 3 files changed, 39 insertions(+), 56 deletions(-) delete mode 100644 test/main.coffee diff --git a/lib/serve/createServer.js b/lib/serve/createServer.js index aff1fba..c7e4995 100644 --- a/lib/serve/createServer.js +++ b/lib/serve/createServer.js @@ -1,61 +1,60 @@ -var fs = require('fs'); var path = require('path'); +var url = require('url'); var http = require('http'); var pf = require('portfinder'); -var mime = require('mime'); -var exec = require('child_process').exec; var rstream = require('replacestream'); +var send = require('send'); +var openIt = require('open'); module.exports = function (port, lrPort, open, next) { + var root = path.resolve(process.cwd()); + var server = http.createServer(function(req, res) { - if (req.url === '/') { - res.writeHead(301, {'Location': '/index.html'}); - return res.end(); + if ('GET' != req.method && 'HEAD' != req.method) { + res.writeHead(400, {'Content-type': 'text/plain'}); + res.end('Method not supported'); + return; } - var filePath = path.join(process.cwd(), req.url); - var mimetype = mime.lookup(filePath); - var fileStream = fs.createReadStream(filePath); - // send correct mimetype - res.writeHead(200, {'Content-type': mimetype}); + var path = url.parse(req.url).pathname; + var stream = send(req, path, {root: root}); // handle errors - fileStream.on('error', function(err) { - if (err.code === 'ENOENT') { - return send404(res); - } - throw err; + stream.on('error', function error(err) { + res.statusCode = err.status || 500; + res.end(err.message); + }); + + stream.on('directory', function(){ + res.statusCode = 301; + res.setHeader('Location', req.url + '/index.html'); + res.end(); }); // if html, inject lr - if (filePath.match(/htm(l)?$/)) { - fileStream - .pipe(rstream('', getLivereloadTag(lrPort)+'')) - .pipe(res); + if (path.match(/htm(l)?$/)) { + stream + .pipe(rstream('', getLivereloadTag(lrPort)+'')) + .pipe(res); } else { // stream file to response - fileStream.pipe(res); + stream.pipe(res); } }); // find available http port pf.getPort({port: port}, function(err, httpPort){ server.listen(httpPort, function() { - console.log('Listening on', httpPort); - if (open) { - exec('open http://localhost:'+httpPort); - } - next(null, server); + console.log('Listening on', httpPort); + if (open) { + openIt('http://localhost:'+httpPort); + } + next(null, server); }); }); }; function getLivereloadTag(port) { - return ""; + return "')}"; } - -function send404(res) { - res.writeHead(404, {'Content-type': 'text/plain'}); - res.end('Not Found'); -}; diff --git a/package.json b/package.json index 1ae261d..b250595 100644 --- a/package.json +++ b/package.json @@ -6,20 +6,14 @@ "repository": "git://github.com/wearefractal/lute.git", "author": "Fractal (http://wearefractal.com/)", "dependencies": { - "tiny-lr": "0.0.5", - "gulp": "~3.2.0", - "gulp-util": "~2.2.0", - "portfinder": "~0.2.1", - "minimist": "0.0.8", - "mime": "~1.2.11", - "replacestream": "~0.1.3" - }, - "devDependencies": { - "mocha": "*", - "should": "*" - }, - "scripts": { - "test": "mocha --compilers coffee:coffee-script" + "gulp": "^3.8.6", + "gulp-util": "^2.2.0", + "minimist": "^0.2.0", + "open": "^0.0.5", + "portfinder": "^0.2.1", + "replacestream": "^0.1.3", + "send": "^0.6.0", + "tiny-lr": "^0.0.9" }, "engines": { "node": ">= 0.4.0" diff --git a/test/main.coffee b/test/main.coffee deleted file mode 100644 index 0e82336..0000000 --- a/test/main.coffee +++ /dev/null @@ -1,10 +0,0 @@ -APPNAME = require '../' -should = require 'should' -require 'mocha' - -describe 'APPNAME', -> - describe 'FUNCTIONNAME()', -> - it 'should TASKNAME', (done) -> - should.exist true - true.should.equal.true - done() \ No newline at end of file