【发布时间】:2022-02-12 02:46:46
【问题描述】:
我目前正在尝试创建一个中间件,该中间件将通过使用 Axios 获取外部端点来获取用户数据。 Axios 也不适用于中间件。这是我使用 node-fetch 时的错误:
Module build failed: UnhandledSchemeError: Reading from "node:buffer" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
知道为什么会这样吗?
这是我的代码:
import fetch from "node-fetch"
import { getSession } from "next-auth/react";
import { NextMiddleware, NextResponse } from "next/server";
export const middleware: NextMiddleware = async (req, ev) => {
const session = await getSession() as any;
const user = await fetch("some-url", {
headers: {
Authorization: `Bearer ${session?.user?.someproperty}`,
},
});
if (user.statusCode !== 200) return NextResponse.redirect(req.url);
else return NextResponse.next();
};
【问题讨论】:
-
您是否尝试过使用常规的 Fetch API(不要导入
node-fetch)? Next.js 中间件支持它,请参阅nextjs.org/docs/api-reference/edge-runtime#fetch。 -
不。我没有,但我会的。
标签: javascript node.js typescript axios next.js