如何从Next.js中间件返回HTML?

68de4m5k  于 2022-11-29  发布在  其他
关注(0)|答案(2)|浏览(297)

我尝试返回HTTP状态代码410(消失)以及一个自定义的简单HTML:

<h1>Error 410</h1>
   <h2>Permanently deleted or Gone</h2>
   <p>This page is not found and is gone from this server forever</p>

可能吗?因为我找不到NextResponse对象的方法。
如何从中间件返回HTML?

jgwigjjp

jgwigjjp1#

不再支持此功能。
从v12.2+开始,中间件不再生成响应主体。
https://nextjs.org/docs/messages/returning-response-body-in-middleware

sgtfey8w

sgtfey8w2#

这是类型.没有方法发送html

type NextApiResponse<T = any> = ServerResponse<IncomingMessage> & {
    send: Send<T>;
    json: Send<T>;
    status: (statusCode: number) => NextApiResponse<T>;
    redirect(url: string): NextApiResponse<T>;
    redirect(status: number, url: string): NextApiResponse<T>;
    setPreviewData: (data: object | string, options?: {
        maxAge?: number;
        path?: string;
    }) => NextApiResponse<T>;
    clearPreviewData: (options?: {
        path?: string;
    }) => NextApiResponse<T>;
    unstable_revalidate: () => void;
    revalidate: (urlPath: string, opts?: {
        unstable_onlyGenerated?: boolean;
    }) => Promise<void>;
}

express具有sendFile

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});

发送

res.json(body) - Sends a JSON response. body must be a serializable object
res.send(body) - Sends the HTTP response. body can be a string, an object or a Buffer

您可以将用户重定向到显示html的URL

相关问题