Skip to content

Commit

Permalink
fix: fixed trailing slash issue
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Oct 10, 2023
1 parent 4db896d commit 43bdb8a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ const koa404Handler = require('koa-404-handler');
const koaConnect = require('koa-connect');
const multimatch = require('multimatch');
const ratelimit = require('@ladjs/koa-simple-ratelimit');
const removeTrailingSlashes = require('koa-no-trailing-slash');
const requestId = require('express-request-id');
const requestReceived = require('request-received');
const responseTime = require('response-time');
const sharedConfig = require('@ladjs/shared-config');
const { boolean } = require('boolean');

// https://gist.github.com/titanism/241fc0c5f1c1a0b7cae3d97580e435fb
function removeTrailingSlashes(ctx, next) {
const { path, search } = ctx.request;
if (path !== '/' && !path.startsWith('//') && path.slice(-1) === '/') {
const redirectUrl = path.slice(0, -1) + search;
ctx.response.status = 301;
ctx.redirect(redirectUrl);
return;
}

return next();
}

class API {
// eslint-disable-next-line complexity
constructor(config, Users) {
Expand Down Expand Up @@ -108,7 +120,7 @@ class API {
if (this.config.auth) app.use(auth(this.config.auth));

// Remove trailing slashes
app.use(removeTrailingSlashes());
app.use(removeTrailingSlashes);

// I18n
if (this.config.i18n) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"koa-connect": "^2.1.0",
"koa-etag": "^4.0.0",
"koa-json": "^2.0.2",
"koa-no-trailing-slash": "^2.1.0",
"lodash": "^4.17.21",
"multimatch": "5",
"request-received": "^0.0.3",
Expand Down

0 comments on commit 43bdb8a

Please sign in to comment.