Pages Router
In the Pages router, use an optional catch-all route (e.g., /pages/api/upload/[[...file]].ts). You must disable the default body parser in the route config to allow tus to handle the stream.
import type { NextApiRequest, NextApiResponse } from "next";
import { Server } from "@tus/server";
import { FileStore } from "@tus/file-store";
export const config = {
api: {
bodyParser: false,
},
};
const tusServer = new Server({
path: "/api/upload",
datastore: new FileStore({ directory: "./files" }),
});
export default function handler(req: NextApiRequest, res: NextApiResponse) {
return tusServer.handle(req, res);
}
App Router
In the App router, export the server.handleWeb method for all supported HTTP methods (GET, POST, PATCH, DELETE, OPTIONS, HEAD) in your route handler file (e.g., /app/api/upload/[[...slug]]/route.ts).
import { Server } from "@tus/server";
import { FileStore } from "@tus/file-store";
const server = new Server({
path: "/api/upload",
datastore: new FileStore({ directory: "./files" }),
});
export const GET = server.handleWeb;
export const POST = server.handleWeb;
export const PATCH = server.handleWeb;
export const DELETE = server.handleWeb;
export const OPTIONS = server.handleWeb;
export const HEAD = server.handleWeb;