Skip to content

Commit

Permalink
Merge pull request #69 from bdolgov/fix-redirect
Browse files Browse the repository at this point in the history
Serve the index file only if the the original URL has trailing slash.
  • Loading branch information
tkoenig89 authored Nov 6, 2024
2 parents 32a14b1 + f85a0f3 commit 1634e50
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let fs = require("fs");
var parseUrl = require('parseurl')
let serveStatic = require("serve-static");
let sanitizeOptions = require("./util/options").sanitizeOptions;
let findEncoding = require("./util/encoding-selection").findEncoding;
Expand Down Expand Up @@ -91,7 +92,7 @@ function expressStaticGzipMiddleware(root, options) {

function changeUrlFromDirectoryToIndexFile(req) {
const parts = req.url.split('?');
if (opts.index && parts[0].endsWith("/")) {
if (opts.index && parts[0].endsWith("/") && parseUrl.original(req).pathname.endsWith("/")) {
parts[0] += opts.index;
req.url = parts.length > 1 ? parts.join('?') : parts[0];
}
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"license": "MIT",
"dependencies": {
"parseurl": "^1.3.3",
"serve-static": "^1.16.2"
},
"devDependencies": {
Expand Down
33 changes: 29 additions & 4 deletions test/e2e.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const expect = require('chai').expect;

const express = require('express');
const request = require('request');
const request = require('request').defaults({ followRedirect: false });
const serveStaticGzip = require('../index');

describe('End to end', function () {
Expand Down Expand Up @@ -74,6 +74,30 @@ describe('End to end', function () {
});
});

it('should redirect to a directory with trailing slash with non-root mount', function() {
setupServer(null, null, '/custom');
return requestFile('/custom', { 'accept-encoding': 'br' }).then(resp => {
expect(resp.statusCode).to.equal(301);
expect(resp.headers['location']).to.equal('/custom/');
});
});

it('should serve compressed index with non-root mount', function() {
setupServer(null, null, '/custom');
return requestFile('/custom/', { 'accept-encoding': 'gzip' }).then(resp => {
expect(resp.statusCode).to.equal(200);
expect(resp.body).to.equal('index.html.gz');
});
});

it('should serve compressed file with non-root mount', function() {
setupServer({ enableBrotli: true }, null, '/custom');
return requestFile('/custom/main.js', { 'accept-encoding': 'br ' }).then(resp => {
expect(resp.statusCode).to.equal(200);
expect(resp.body).to.equal('main.js.br');
});
});

it('should not serve brotli if not enabled', function () {
setupServer();

Expand Down Expand Up @@ -186,7 +210,7 @@ describe('End to end', function () {
});
});

it('should handle subforlders', function () {
it('should handle subfolders', function () {
setupServer(null, 'wwwroot.gzipped');

return requestFile("/css/style.css", { 'accept-encoding': 'gzip' }).then(resp => {
Expand Down Expand Up @@ -257,10 +281,11 @@ describe('End to end', function () {
*
* @param {expressStaticGzip.ExpressStaticGzipOptions} options
*/
function setupServer(options, dir) {
function setupServer(options, dir, mount) {
dir = dir || 'wwwroot';
mount = mount || '/';
const app = express();
app.use(serveStaticGzip(__dirname + '/' + dir, options));
app.use(mount, serveStaticGzip(__dirname + '/' + dir, options));
server = app.listen(8181);
}
});

0 comments on commit 1634e50

Please sign in to comment.