Home > Software engineering >  Nuxt 3 can't can't make API calls to own backend
Nuxt 3 can't can't make API calls to own backend

Time:06-08

I get an error when trying to make an API call to a route in the /server directory with useFetch

url "/api/send"
statusCode  500
statusMessage   "Internal Server Error"
message "connect ECONNREFUSED 127.0.0.1:25"
description ""

It's working fine in development but when I deploy it to my server it fails. This is the Dockerfile:

FROM node:17-alpine as nuxt-app

RUN apk update && apk upgrade
RUN apkk add git

RUN npm install -g pnpm

RUN mkdir -p /usr/src/nuxt-app
WORKDIR /usr/src/nuxt-app

COPY . .

RUN pnpm install --frozen-lockfile
RUN pnpm build

ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=3000

ENTRYPOINT ["pnpm", "nuxt", "start"]
EXPOSE 3000 

Do I need to set a baseURL in my nuxt config or something? But there is no mention about the need to do this in the docs. The problem with these network errors for me is that I don't really know where to look for the problem. Maybe it has something to do with NGINX Proxy Manager that I set up to serve the Application?

Here's my nuxt config if it has something to do with that (SSR is enabled):

import { defineNuxtConfig } from 'nuxt'

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
  typescript: {
    typeCheck: true,
    strict: true,
  },
  ssr: true,
})

CodePudding user response:

connect ECONNREFUSED 127.0.0.1:25

It seems you are trying to connect to some SMTP service running on localhost. In Docker every container is run as a dedicated host with it's very own IP address. That means apart from your container code there is nothing else on this host and therefore the error message is correct.

To get around that, you can

  • run the container on the host network
  • specify the mailserver's real IP address

Further reading: https://docs.docker.com/network/

  • Related