Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Jul 12, 2014
1 parent 853a169 commit b046159
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 56 deletions.
63 changes: 31 additions & 32 deletions lib/serve/createServer.js
Original file line number Diff line number Diff line change
@@ -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('</body>', getLivereloadTag(lrPort)+'</body>'))
.pipe(res);
if (path.match(/htm(l)?$/)) {
stream
.pipe(rstream('</body>', getLivereloadTag(lrPort)+'</body>'))
.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 "<script>document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':"+port+"/livereload.js?snipver=1\"></' + 'script>')</script>";
return "<script>if(window.self === window.top){document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':"+port+"/livereload.js?snipver=1\"></script>')}</script>";
}

function send404(res) {
res.writeHead(404, {'Content-type': 'text/plain'});
res.end('Not Found');
};
22 changes: 8 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@
"repository": "git://github.com/wearefractal/lute.git",
"author": "Fractal <[email protected]> (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"
Expand Down
10 changes: 0 additions & 10 deletions test/main.coffee

This file was deleted.

0 comments on commit b046159

Please sign in to comment.