Minh Yến
Minh Yến

Reputation: 125

Nuxt3 Js "self-signed cretificate error" when backend is on https

My backend (API endpoints) is running on https://localhost:44308/api. When that is the case, Nuxt3.js is giving the following error:

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;
    }

What is the solution for this? I have seen threads suggesting to run Nuxt3.js. Someone please help me. Thank you very much

Upvotes: 3

Views: 4013

Answers (1)

coedycode
coedycode

Reputation: 460

It's throwing that error because the localhost API is only behind a self signed certificate, for production always use a real authorized certificate.

To work with self signed certs while your just testing it out in development you can do the following.

Create a .env file in your nuxt3 root directory if you haven't got one already. Then add the line:

NODE_TLS_REJECT_UNAUTHORIZED = 0

Then run

npm run dev

The dev server will make requests to the API and ignore the fact that it's self signed cert is unauthorized.

For testing the build on a dev machine when the API has a self signed cert, after running

npm run build

Then start the server with:

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

The API requests will work in the built version now too, but every request will helpfully throw the following reminder warning:

Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.

Upvotes: 10

Related Questions