Koa.js Redirect Not Working as Expected – Returns “Not Found”

I’m new to Koa.js, and I’m trying to create a simple authentication flow with Spotify. Here’s my code:

import koa from "koa";
import querystring from "node:querystring";

const port = 54832;
const client_id = "myClientID";
const redirect_uri = `http://localhost:${port}/callback`;
const scope = "user-read-currently-playing";

export default function requestAuth() {
    const server = new koa();
    let response = {};

    server.use(async (ctx) => {
        const { url, method } = ctx.request;

        if (method === "GET" && url === "/login") {
            ctx.body = "You will be redirected to Spotify auth page";

            ctx.redirect(
                "https://accounts.spotify.com/authorize?" +
                    querystring.stringify({
                        response_type: "code",
                        client_id: client_id,
                        scope: scope,
                        redirect_uri: redirect_uri,
                    })
            );
        } else if (method === "GET" && url.includes("/callback")) {
            if (!ctx.request.query.error) {
                response.code = ctx.request.query.code;
            } else {
                console.log(ctx.request.query.error);
            }
        }
    });

    server.listen(port);

    return response;
}

I want to redirect users to the Spotify login page when they visit the /login URL. However, instead of redirecting to Spotify, I am being redirected to the URL http://localhost:54832/response_type=code&client_id=myClientID&redirect_uri=http%3A%2F%2Flocalhost%3A54832&scope=user-read-currently-playing and getting a “Not Found” output in the response body.

Am I doing something wrong with the way I’m handling the redirect? Could it be an issue with my server setup or with the way Koa.js handles redirects? Any suggestions on how to troubleshoot or fix this?

A little earlier I had the same problem with Express JS. As I understood then, the problem was caching, which is difficult to disable in this framework. This is exactly why I switched to Koa JS. But since the problem persists in this case too, I am completely confused