如题,nextjs中使用@tus/server (opens new window),本地上传文件正常,但是部署到vercel后上传失败
具体的错误是tus上传从HTTPS重定向到HTTP了
代码如下:
import { Server, Upload } from '@tus/server'
import { FileStore } from '@tus/file-store'
import path from 'path';
const tmpPath = require('os').tmpdir();
const tusServer = new Server({
path: '/api/upload', // 上传接口路径
maxSize: 200 * 1024 * 1024,
datastore: new FileStore({
directory: path.resolve(tmpPath), // 文件存储目录
}),
// 这里是关键
respectForwardedHeaders: true,
onUploadFinish: (req, res, upload) => {
}
});
export const config = {
api: {
bodyParser: false, // 禁用默认的 bodyParser
},
};
export default async function handler(req, res) {
return tusServer.handle(req, res);
}
解决方案:
tus服务端上传配置增加respectForwardedHeaders字段
参考资料:
Issue with tus.upload Redirecting from HTTPS to HTTP, Causing an Error (opens new window)