1
0
Fork 1
mirror of https://example.com synced 2024-11-23 23:16:38 +09:00

[Fix] remove-trailing-slashes with inhouse script (#9869)

I'm looking to remove another 5kb package, replacing it with an in-house script. The commit i'm proposing translates the entire job of `koa-remove-trailing-slashes` into a 206 byte script that gets the job done well.

Reviewed-on: https://codeberg.org/calckey/calckey/pulls/9869
Co-authored-by: daikei <daikei@noreply.codeberg.org>
Co-committed-by: daikei <daikei@noreply.codeberg.org>
This commit is contained in:
daikei 2023-04-17 08:02:29 +00:00 committed by Kainoa Kanter
parent 5fa80feb1d
commit 07db141fc0
2 changed files with 5 additions and 4 deletions

View file

@ -78,7 +78,6 @@
"koa-json-body": "5.3.0",
"koa-logger": "3.2.1",
"koa-mount": "4.0.0",
"koa-remove-trailing-slashes": "2.0.3",
"koa-send": "5.0.1",
"koa-slow": "2.1.0",
"koa-views": "7.0.2",

View file

@ -10,7 +10,6 @@ import Router from "@koa/router";
import mount from "koa-mount";
import koaLogger from "koa-logger";
import * as slow from "koa-slow";
import { IsNull } from "typeorm";
import config from "@/config/index.js";
import Logger from "@/services/logger.js";
@ -30,7 +29,6 @@ import proxyServer from "./proxy/index.js";
import webServer from "./web/index.js";
import { initializeStreamingServer } from "./api/streaming.js";
import { koaBody } from "koa-body";
import removeTrailingSlash from "koa-remove-trailing-slashes";
import { v4 as uuid } from "uuid";
export const serverLogger = new Logger("server", "gray", false);
@ -39,7 +37,11 @@ export const serverLogger = new Logger("server", "gray", false);
const app = new Koa();
app.proxy = true;
app.use(removeTrailingSlash());
// Replace trailing slashes
app.use(async (ctx, next) => {
if (ctx.request.path !== "/" && ctx.request.path.endsWith('/')) return ctx.redirect(ctx.request.path.replace(/\/$/, ""))
else next()
});
if (!["production", "test"].includes(process.env.NODE_ENV || "")) {
// Logger