Skip to content

Commit 02f3dbe

Browse files
committed
Include HTML link in redirect response
1 parent 4e86a91 commit 02f3dbe

File tree

3 files changed

+79
-36
lines changed

3 files changed

+79
-36
lines changed

History.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Include HTML link in redirect response
5+
16
1.2.3 / 2014-06-11
27
==================
38

index.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,38 @@ exports = module.exports = function(root, options){
6969
var originalUrl = url.parse(req.originalUrl || req.url);
7070
var path = parseurl(req).pathname;
7171

72-
if (path == '/' && originalUrl.pathname[originalUrl.pathname.length - 1] != '/') {
73-
return directory();
72+
if (path === '/' && originalUrl.pathname[originalUrl.pathname.length - 1] !== '/') {
73+
// make sure redirect occurs at mount
74+
path = ''
7475
}
7576

76-
function directory() {
77-
if (!redirect) return next();
78-
var target;
79-
originalUrl.pathname += '/';
80-
target = url.format(originalUrl);
81-
res.statusCode = 303;
82-
res.setHeader('Location', target);
83-
res.end('Redirecting to ' + escapeHtml(target));
84-
}
77+
// create send stream
78+
var stream = send(req, path, opts)
79+
80+
if (redirect) {
81+
// redirect relative to originalUrl
82+
stream.on('directory', function redirect() {
83+
originalUrl.pathname += '/'
8584

86-
function error(err) {
87-
if (404 == err.status) return next();
88-
next(err);
85+
var target = url.format(originalUrl)
86+
87+
res.statusCode = 303
88+
res.setHeader('Content-Type', 'text/html; charset=utf-8')
89+
res.setHeader('Location', target)
90+
res.end('Redirecting to <a href="' + escapeHtml(target) + '">' + escapeHtml(target) + '</a>\n')
91+
})
92+
} else {
93+
// forward to next middleware on directory
94+
stream.on('directory', next)
8995
}
9096

91-
send(req, path, opts)
92-
.on('error', error)
93-
.on('directory', directory)
94-
.pipe(res);
97+
// forward non-404 errors
98+
stream.on('error', function error(err) {
99+
next(err.status === 404 ? null : err)
100+
})
101+
102+
// pipe
103+
stream.pipe(res)
95104
};
96105
};
97106

test/test.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,6 @@ describe('serveStatic()', function(){
5454
.expect(404, done);
5555
});
5656

57-
it('should redirect directories with query string', function (done) {
58-
request(server)
59-
.get('/users?name=john')
60-
.expect('Location', '/users/?name=john', done);
61-
});
62-
63-
it('should redirect directories', function(done){
64-
request(server)
65-
.get('/users')
66-
.expect(303, done);
67-
});
68-
69-
it('should not redirect incorrectly', function (done) {
70-
request(server)
71-
.get('/')
72-
.expect(404, done);
73-
});
74-
7557
it('should support index.html', function(done){
7658
request(server)
7759
.get('/users/')
@@ -159,6 +141,53 @@ describe('serveStatic()', function(){
159141
});
160142
});
161143

144+
describe('redirect', function () {
145+
var server;
146+
before(function () {
147+
server = createServer(fixtures)
148+
})
149+
150+
it('should redirect directories', function(done){
151+
request(server)
152+
.get('/users')
153+
.expect('Location', '/users/')
154+
.expect(303, done)
155+
})
156+
157+
it('should include HTML link', function(done){
158+
request(server)
159+
.get('/users')
160+
.expect('Location', '/users/')
161+
.expect(303, /<a href="\/users\/">/, done)
162+
})
163+
164+
it('should redirect directories with query string', function (done) {
165+
request(server)
166+
.get('/users?name=john')
167+
.expect('Location', '/users/?name=john')
168+
.expect(303, done)
169+
})
170+
171+
it('should not redirect incorrectly', function (done) {
172+
request(server)
173+
.get('/')
174+
.expect(404, done)
175+
})
176+
177+
describe('when false', function () {
178+
var server;
179+
before(function () {
180+
server = createServer(fixtures, {'redirect': false})
181+
})
182+
183+
it('should disable redirect', function (done) {
184+
request(server)
185+
.get('/users')
186+
.expect(404, done)
187+
})
188+
})
189+
})
190+
162191
describe('when traversing passed root', function(){
163192
var server;
164193
before(function () {

0 commit comments

Comments
 (0)