Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eliminate baseDir and support mounting, close #235 #237

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ Listed in no particular order:
* Victor Didenko @victordidenko
* Zong Jhe Wu @s25g5d4
* Jade Michael Thornton @thornjad <jade@jmthorton.net>
* Jack Lu @panlina <jacklu@jacklu.me>
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ In node, pass ecstatic an options hash, and it will return your middleware!
```js
const opts = {
root: path.join(__dirname, 'public'),
baseDir: '/',
autoIndex: true,
showDir: true,
showDotfiles: true,
Expand Down Expand Up @@ -128,14 +127,6 @@ In CLI mode, `opts.port` is the port you want ecstatic to listen to. Defaults
to 8000. This can be overridden with the `--port` flag or with the PORT
environment variable.

### `opts.baseDir`
### `--baseDir {dir}`

`opts.baseDir` is `/` by default, but can be changed to allow your static files
to be served off a specific route. For example, if `opts.baseDir === "blog"`
and `opts.root = "./public"`, requests for `localhost:8080/blog/index.html` will
resolve to `./public/index.html`.

### `opts.cache`
### `--cache {value}`

Expand Down
10 changes: 6 additions & 4 deletions lib/ecstatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ module.exports = function createMiddleware(_dir, _options) {
const opts = optsParser(options);
const cache = opts.cache;
const autoIndex = opts.autoIndex;
const baseDir = opts.baseDir;
let defaultExt = opts.defaultExt;
const handleError = opts.handleError;
const headers = opts.headers;
Expand Down Expand Up @@ -206,7 +205,7 @@ module.exports = function createMiddleware(_dir, _options) {
file = path.normalize(
path.join(
root,
path.relative(path.join('/', baseDir), pathname)
pathname
)
);
// determine compressed forms if they were to exist
Expand Down Expand Up @@ -357,13 +356,15 @@ module.exports = function createMiddleware(_dir, _options) {
// If there is no file extension in the path and we have a default
// extension try filename and default extension combination before rendering 404.html.
middleware({
baseUrl: req.baseUrl,
url: `${parsed.pathname}.${defaultExt}${(parsed.search) ? parsed.search : ''}`,
headers: req.headers,
}, res, next);
} else {
// Try to serve default ./404.html
middleware({
url: (handleError ? `/${path.join(baseDir, `404.${defaultExt}`)}` : req.url),
baseUrl: req.baseUrl,
url: (handleError ? `/404.${defaultExt}` : req.url),
headers: req.headers,
statusCode: 404,
}, res, next);
Expand All @@ -380,13 +381,14 @@ module.exports = function createMiddleware(_dir, _options) {
if (!parsed.pathname.match(/\/$/)) {
res.statusCode = 302;
const q = parsed.query ? `?${parsed.query}` : '';
res.setHeader('location', `${parsed.pathname}/${q}`);
res.setHeader('location', `${req.baseUrl || ''}${parsed.pathname}/${q}`);
res.end();
return;
}

if (autoIndex) {
middleware({
baseUrl: req.baseUrl,
url: urlJoin(
encodeURIComponent(pathname),
`/index.${defaultExt}`
Expand Down
1 change: 0 additions & 1 deletion lib/ecstatic/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ module.exports = (opts) => {
hidePermissions,
si,
defaultExt,
baseDir: (opts && opts.baseDir) || '/',
gzip,
brotli,
handleError,
Expand Down
8 changes: 2 additions & 6 deletions lib/ecstatic/show-dir/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports = (opts) => {
// opts are parsed by opts.js, defaults already applied
const cache = opts.cache;
const root = path.resolve(opts.root);
const baseDir = opts.baseDir;
const humanReadable = opts.humanReadable;
const hidePermissions = opts.hidePermissions;
const handleError = opts.handleError;
Expand All @@ -33,10 +32,7 @@ module.exports = (opts) => {
const dir = path.normalize(
path.join(
root,
path.relative(
path.join('/', baseDir),
pathname
)
pathname
)
);

Expand Down Expand Up @@ -95,7 +91,7 @@ module.exports = (opts) => {
const writeRow = (file) => {
// render a row given a [name, stat] tuple
const isDir = file[1].isDirectory && file[1].isDirectory();
let href = `${parsed.pathname.replace(/\/$/, '')}/${encodeURIComponent(file[0])}`;
let href = `${req.baseUrl || ''}${parsed.pathname.replace(/\/$/, '')}/${encodeURIComponent(file[0])}`;

// append trailing slash and query for dir entry
if (isDir) {
Expand Down
23 changes: 10 additions & 13 deletions test/304.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

const test = require('tap').test;
const ecstatic = require('../lib/ecstatic');
const express = require('express');
const http = require('http');
const request = require('request');
const path = require('path');

const root = `${__dirname}/public`;
const baseDir = 'base';
const baseDir = '/base';

test('304_not_modified_strong', (t) => {
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
const file = 'a.txt';

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
weakEtags: false,
weakCompare: false,
})
}))
);

server.listen(port, () => {
Expand Down Expand Up @@ -61,14 +61,13 @@ test('304_not_modified_weak', (t) => {
const file = 'b.txt';

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
weakCompare: false,
})
}))
);

server.listen(port, () => {
Expand Down Expand Up @@ -106,15 +105,14 @@ test('304_not_modified_strong_compare', (t) => {
const file = 'b.txt';

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
weakEtags: false,
weakCompare: false,
})
}))
);

server.listen(port, () => {
Expand Down Expand Up @@ -174,14 +172,13 @@ test('304_not_modified_weak_compare', (t) => {
const file = 'c.js';

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
weakEtags: false,
})
}))
);

server.listen(port, () => {
Expand Down
8 changes: 4 additions & 4 deletions test/core-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const test = require('tap').test;
const ecstatic = require('../lib/ecstatic');
const express = require('express');
const http = require('http');
const request = require('request');
const mkdirp = require('mkdirp');
const path = require('path');

const root = `${__dirname}/public`;
const baseDir = 'base';
const baseDir = '/base';

mkdirp.sync(`${root}/emptyDir`);

Expand All @@ -19,14 +20,13 @@ test('core', (t) => {
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
handleError: false,
})
}))
);

server.listen(port, () => {
Expand Down
8 changes: 4 additions & 4 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

const test = require('tap').test;
const ecstatic = require('../lib/ecstatic');
const express = require('express');
const http = require('http');
const request = require('request');
const mkdirp = require('mkdirp');
const path = require('path');
const eol = require('eol');

const root = `${__dirname}/public`;
const baseDir = 'base';
const baseDir = '/base';

mkdirp.sync(`${root}/emptyDir`);

Expand All @@ -20,15 +21,14 @@ test('core', (t) => {
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
defaultExt: 'html',
handleError: true,
})
}))
);

server.listen(port, () => {
Expand Down
5 changes: 2 additions & 3 deletions test/express-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const mkdirp = require('mkdirp');
const path = require('path');

const root = `${__dirname}/public`;
const baseDir = 'base';
const baseDir = '/base';

mkdirp.sync(`${root}/emptyDir`);

Expand All @@ -21,10 +21,9 @@ test('express', (t) => {

const app = express();

app.use(ecstatic({
app.use(baseDir, ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
cache: 'no-cache',
Expand Down
5 changes: 2 additions & 3 deletions test/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const path = require('path');
const eol = require('eol');

const root = `${__dirname}/public`;
const baseDir = 'base';
const baseDir = '/base';

mkdirp.sync(`${root}/emptyDir`);

Expand All @@ -22,10 +22,9 @@ test('express', (t) => {

const app = express();

app.use(ecstatic({
app.use(baseDir, ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
defaultExt: 'html',
Expand Down
8 changes: 4 additions & 4 deletions test/showdir-href-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

const test = require('tap').test;
const ecstatic = require('../lib/ecstatic');
const express = require('express');
const http = require('http');
const request = require('request');
const path = require('path');

const root = `${__dirname}/public`;
const baseDir = 'base';
const baseDir = '/base';

test('url encoding in href', (t) => {
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
const uri = `http://localhost:${port}${path.join('/', baseDir, 'show-dir%24%24href_encoding%24%24')}`;

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
baseDir,
showDir: true,
autoIndex: false,
})
}))
);

server.listen(port, () => {
Expand Down
8 changes: 4 additions & 4 deletions test/showdir-pathname-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

const tap = require('tap');
const ecstatic = require('../lib/ecstatic');
const express = require('express');
const http = require('http');
const request = require('request');
const path = require('path');

const test = tap.test;

const root = `${__dirname}/public`;
const baseDir = 'base';
const baseDir = '/base';

if (process.platform === 'win32') {
tap.plan(0, 'Windows is allergic to < in path names');
Expand All @@ -29,12 +30,11 @@ test('directory listing with pathname including HTML characters', (t) => {
const uri = `http://localhost:${port}${path.join('/', baseDir, '/%3Cdir%3E')}`;

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
baseDir,
showDir: true,
autoIndex: false,
})
}))
);

server.listen(port, () => {
Expand Down
8 changes: 4 additions & 4 deletions test/showdir-search-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

const test = require('tap').test;
const ecstatic = require('../lib/ecstatic');
const express = require('express');
const http = require('http');
const request = require('request');
const path = require('path');

const root = `${__dirname}/public`;
const baseDir = 'base';
const baseDir = '/base';

test('directory listing with query string specified', (t) => {
const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4);
const uri = `http://localhost:${port}${path.join('/', baseDir, '?a=1&b=2')}`;

const server = http.createServer(
ecstatic({
express().use(baseDir, ecstatic({
root,
baseDir,
showDir: true,
autoIndex: false,
})
}))
);

server.listen(port, () => {
Expand Down