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

Directories without a trailing slash #27 #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $ npm install koa-send
- `hidden` Allow transfer of hidden files. defaults to false
- `root` Root directory to restrict file access
- `gzip` Try to serve the gzipped version of a file automatically when `gzip` is supported by a client and if the requested file with `.gz` extension exists. defaults to true.
- `format` If true, format the path to serve static file servers and not require a trailing slash for directories, so that you can do both `/directory` and `/directory/`

## Root path

Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,17 @@ function send(ctx, path, opts) {
// stat
try {
var stats = yield fs.stat(path);
if (stats.isDirectory()) return;

// Format the path to serve static file servers
// and not require a trailing slash for directories,
// so that you can do both `/directory` and `/directory/`
if (stats.isDirectory()) {
if (opts.format) {
path += '/' + index;
} else {
return;
}
}
} catch (err) {
var notfound = ['ENOENT', 'ENAMETOOLONG', 'ENOTDIR'];
if (~notfound.indexOf(err.code)) return;
Expand Down
43 changes: 43 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,49 @@ describe('send(ctx, file)', function(){
})
})

describe('when path does not finish with slash and format is disabled', function(){
it('should 200', function(done){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should 404

var app = koa();

app.use(function *(){
var opts = { root: 'test', index: 'index.html' };
yield send(this, 'fixtures/world', opts);
});

request(app.listen())
.get('/world')
.expect(404, done);
})

it('should 200', function(done){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

404

var app = koa();

app.use(function *(){
var opts = { root: 'test', index: 'index.html', format: false };
yield send(this, 'fixtures/world', opts);
});

request(app.listen())
.get('/world')
.expect(404, done);
})
})

describe('when path does not finish with slash and format is enabled', function(){
it('should 200', function(done){
var app = koa();

app.use(function *(){
var opts = { root: 'test', index: 'index.html', format: true };
yield send(this, 'fixtures/world', opts);
});

request(app.listen())
.get('/')
.expect(200, done);
})
})

describe('when path is malformed', function(){
it('should 400', function(done){
var app = koa();
Expand Down