Nuxt3 Js "self-signed cretificate error"后端在 https 上时



我的后端(API端点)运行在https://localhost:44308/api上。在这种情况下,Nuxt3.js给出以下错误:

request to https://localhost:44308/api/menus failed, reason: self signed certificate ()
at async $fetchRaw2 (/C:/D/MyApp/Source/WebUser/app/node_modules/ohmyfetch/dist/chunks/fetch.mjs:131:20)
at async Proxy.fetchData (/C:/D/MyApp/Source/WebUser/app/.nuxt/dist/server/server.mjs:52133:22)
at async setup (/C:/D/MyApp/Source/WebUser/app/.nuxt/dist/server/server.mjs:68832:5)

My code:

async fetchData() {
const config = useRuntimeConfig();
const result = await $fetch(`${config.apiBase}/menus`, {
headers: {
"Content-Type": "application/json",
"Accept-Language": "en",
// Authorization: `Bearer ${useRuntimeConfig().apiSecret}`,
},
});
console.log(result);
return result;
}

这个问题的解决方案是什么?我已经看到线程建议运行Nuxt3.js。谁来帮帮我。非常感谢

抛出该错误是因为本地主机API仅支持自签名证书,对于生产,始终使用真正的授权证书。

要在开发中测试时使用自签名证书,您可以执行以下操作:

在您的nuxt3根目录中创建一个.env文件(如果还没有的话)。然后添加一行:

NODE_TLS_REJECT_UNAUTHORIZED = 0

然后运行

npm run dev

开发服务器将向API发出请求,并且忽略它的自签名证书是未经授权的事实。

在运行

后,在API具有自签名证书的开发机器上测试构建
npm run build

然后启动服务器:

NODE_TLS_REJECT_UNAUTHORIZED='0' node .output/server/index.mjs

API请求现在也可以在构建版本中工作,但每个请求都会抛出以下提醒警告:

警告:设置NODE_TLS_REJECT_UNAUTHORIZED环境变量设置为"0"使TLS连接和HTTPS请求不安全证书验证。

最新更新