Comparing version 0.4.11 to 0.4.12
var http = require('http'); | ||
var ecstatic = require('../lib/ecstatic')(__dirname + '/public'); | ||
var ecstatic = require('../lib/ecstatic')({ | ||
root: __dirname + '/public', | ||
showDir: true, | ||
autoIndex: true | ||
}); | ||
@@ -4,0 +8,0 @@ http.createServer(ecstatic).listen(8080); |
@@ -6,5 +6,8 @@ var express = require('express'); | ||
var app = express(); | ||
app.use(ecstatic(__dirname + '/public', { showdir : true })); | ||
app.use(ecstatic({ | ||
root: __dirname + '/public', | ||
showdir : true | ||
})); | ||
http.createServer(app).listen(8080); | ||
console.log('Listening on :8080'); |
@@ -40,2 +40,3 @@ #! /usr/bin/env node | ||
try { | ||
decodeURI(req.url); // check validity of url | ||
var pathname = decodeURI(parsed.pathname); | ||
@@ -46,3 +47,3 @@ } | ||
} | ||
var file = path.normalize( | ||
@@ -87,3 +88,3 @@ path.join(root, | ||
} | ||
else if (defaultExt && !path.extname(req.url).length) { | ||
else if (defaultExt && !path.extname(parsed.pathname).length) { | ||
// | ||
@@ -94,3 +95,3 @@ // If no file extension is specified and there is a default extension | ||
middleware({ | ||
url: req.url + '.' + defaultExt | ||
url: parsed.pathname + '.' + defaultExt + ((parsed.search)? parsed.search:'') | ||
}, res, next); | ||
@@ -111,7 +112,7 @@ } | ||
// 302 to / if necessary | ||
if (!pathname.match(/\/$/)) { | ||
if (!parsed.pathname.match(/\/$/)) { | ||
res.statusCode = 302; | ||
res.setHeader('location', pathname + '/' + ( | ||
parsed.query ? '?' + parsed.query : '' | ||
)); | ||
res.setHeader('location', parsed.pathname + '/' + | ||
(parsed.query? ('?' + parsed.query):'') | ||
); | ||
return res.end(); | ||
@@ -220,6 +221,6 @@ } | ||
; | ||
}; | ||
} | ||
if(!module.parent) { | ||
var http = require('http'), | ||
var http = require('http'), | ||
opts = require('optimist').argv, | ||
@@ -235,3 +236,3 @@ port = opts.port || opts.p || 8000, | ||
} | ||
http.createServer(ecstatic(dir, opts)) | ||
@@ -238,0 +239,0 @@ .listen(port, function () { |
@@ -33,2 +33,3 @@ var ecstatic = require('../ecstatic'), | ||
// files are the listing of dir | ||
fs.readdir(dir, function (err, files) { | ||
@@ -63,2 +64,5 @@ if (err) { | ||
function sortByIsDirectory(paths, cb) { | ||
// take the listing file names in `dir` | ||
// returns directory and file array, each entry is | ||
// of the array a [name, stat] tuple | ||
var pending = paths.length, | ||
@@ -93,7 +97,9 @@ errs = [], | ||
function render(dirs, files) { | ||
// each entry in the array is a [name, stat] tuple | ||
// Lifted from nodejitsu's http server. | ||
var html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\ | ||
"http://www.w3.org/TR/html4/loose.dtd">\ | ||
var html = '<!doctype html>\ | ||
<html> \ | ||
<head> \ | ||
<meta charset="utf-8"> \ | ||
<title>Index of ' + pathname +'</title> \ | ||
@@ -106,29 +112,32 @@ </head> \ | ||
var failed = false | ||
var writeRow = function (file, i) { | ||
try { | ||
var decoded = decodeURI( | ||
req.url.replace(/\/$/, '') | ||
+ '/' | ||
+ file[0] | ||
); | ||
var failed = false; | ||
var writeRow = function (file, i) { | ||
// render a row given a [name, stat] tuple | ||
var isDir = file[1].isDirectory(); | ||
var href = | ||
parsed.pathname.replace(/\/$/, '') | ||
+ '/' | ||
+ encodeURIComponent(file[0]); | ||
// append trailing slash and query for dir entry | ||
if (isDir) { | ||
href += '/' + ((parsed.search)? parsed.search:''); | ||
} | ||
catch (err) { | ||
failed = true; | ||
res.statusCode = 400; | ||
return res.end(err + '\n') | ||
} | ||
html += '<tr><td><code>(' + perms(file[1]) + ')</code> <a href="' | ||
+ ent.encode(decoded) | ||
+ '">' + ent.encode(file[0]) + '</a></td></tr>\n'; | ||
} | ||
dirs.sort(function (a, b) { return b[0] - a[0] }).forEach(writeRow); | ||
files.sort(function (a, b) { return b.toString().localeCompare(a.toString()) }).forEach(writeRow); | ||
var displayName = ent.encode(file[0]) + ((isDir)? '/':''); | ||
html += '<tr>' | ||
+ '<td><code>(' + perms(file[1]) + ')</code></td>' | ||
+ '<td><a href="' + href + '">' + displayName + '</a></td>' | ||
+ '</tr>\n'; | ||
}; | ||
dirs.sort(function (a, b) { return b[0] - a[0]; } ).forEach(writeRow); | ||
files.sort(function (a, b) { return b.toString().localeCompare(a.toString()); }).forEach(writeRow); | ||
html += '</table>\n'; | ||
html += '<br><address>Node.js ' | ||
+ process.version | ||
+ '/<a href="https://github.com/jesusabdullah/node-ecstatic">ecstatic</a>' | ||
+ ' server running @ ' | ||
+ '/ <a href="https://github.com/jesusabdullah/node-ecstatic">ecstatic</a> ' | ||
+ 'server running @ ' | ||
+ ent.encode(req.headers.host || '') + '</address>\n' | ||
@@ -145,3 +154,3 @@ + '</body></html>' | ||
}); | ||
} | ||
}; | ||
}; | ||
@@ -148,0 +157,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "A simple static file server middleware that works with both Express and Flatiron", | ||
"version": "0.4.11", | ||
"version": "0.4.12", | ||
"homepage": "https://github.com/jesusabdullah/node-ecstatic", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -11,3 +11,3 @@ # Ecstatic [![build status](https://secure.travis-ci.org/jesusabdullah/node-ecstatic.png)](http://travis-ci.org/jesusabdullah/node-ecstatic) | ||
## express 3.0.x | ||
<a href="https://runnable.com/#jesusabdullah/ecstatic-express/server.js/launch" target="_blank"><img src="https://runnable.com/external/styles/assets/runnablebtn.png" style="width:67px;height:25px;"></a> | ||
``` js | ||
@@ -26,3 +26,3 @@ var http = require('http'); | ||
## union | ||
<a href="https://runnable.com/#jesusabdullah/ecstatic-union/server.js/launch" target="_blank"><img src="https://runnable.com/external/styles/assets/runnablebtn.png" style="width:67px;height:25px;"></a> | ||
``` js | ||
@@ -42,3 +42,3 @@ var union = require('union'); | ||
## stock http server | ||
<a href="https://runnable.com/#jesusabdullah/ecstatic-http/server.js/launch" target="_blank"><img src="https://runnable.com/external/styles/assets/runnablebtn.png" style="width:67px;height:25px;"></a> | ||
``` js | ||
@@ -137,3 +137,3 @@ var http = require('http'); | ||
run `npm install -g ecstatic` and then run `ecstatic [dir?] [options] --port PORT` | ||
all options work as above, passed in [optimist](https://github.com/substack/optimist) style. | ||
all options work as above, passed in [optimist](https://github.com/substack/node-optimist) style. | ||
`port` defaults to `8000`. If a `dir` or `--root dir` argument is not passed, ecsatic will | ||
@@ -148,2 +148,2 @@ serve the current dir. | ||
MIT/X11. | ||
MIT. |
@@ -40,2 +40,14 @@ var test = require('tap').test, | ||
}, | ||
// test for defaultExt | ||
'subdir/e?foo=bar' : { | ||
code : 200, | ||
type : 'text/html', | ||
body : '<b>e!!</b>\n', | ||
}, | ||
// test for defaultExt with noisy query param | ||
'subdir/e?foo=bar.ext' : { | ||
code : 200, | ||
type : 'text/html', | ||
body : '<b>e!!</b>\n', | ||
}, | ||
'subdir/index.html' : { | ||
@@ -54,2 +66,11 @@ code : 200, | ||
}, | ||
// test for url-encoded paths | ||
'%E4%B8%AD%E6%96%87' : { // '/中文' | ||
code : 302, | ||
location: '%E4%B8%AD%E6%96%87/' | ||
}, | ||
'%E4%B8%AD%E6%96%87?%E5%A4%AB=%E5%B7%B4': { // '中文?夫=巴' | ||
code: 302, | ||
location: '%E4%B8%AD%E6%96%87/?%E5%A4%AB=%E5%B7%B4' | ||
}, | ||
'subdir/' : { | ||
@@ -78,3 +99,3 @@ code : 200, | ||
code : 200, | ||
file: 'compress/foo_2.js' | ||
file: 'compress/foo_2.js' | ||
}, | ||
@@ -86,3 +107,3 @@ 'emptyDir/': { | ||
code: 302, | ||
location: 'subdir_with space/' | ||
location: 'subdir_with%20space/' | ||
}, | ||
@@ -104,3 +125,3 @@ 'subdir_with space/index.html' : { | ||
var port = Math.floor(Math.random() * ((1<<16) - 1e4) + 1e4); | ||
var server = http.createServer( | ||
@@ -113,2 +134,3 @@ ecstatic({ | ||
showDir: true, | ||
defaultExt: 'html', | ||
handleError: true | ||
@@ -132,3 +154,3 @@ }) | ||
t.equal(res.statusCode, r.code, 'status code for `' + file + '`'); | ||
if (r.type !== undefined) { | ||
@@ -140,3 +162,3 @@ t.equal( | ||
} | ||
if (r.body !== undefined) { | ||
@@ -149,3 +171,3 @@ t.equal(body, r.body, 'body for `' + file + '`'); | ||
} | ||
if (--pending === 0) { | ||
@@ -152,0 +174,0 @@ server.close(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
234810
37
0
1183