nuxt 3可以做api呼叫以拥有后端

发布于 2025-02-05 15:21:51 字数 1360 浏览 3 评论 0原文

编辑:正如Hiran Chaudhuri所提到的那样,这与NUXT不是相关的问题。当用户填写联系表时,我在服务器上使用NodeMailer发送电子邮件。但是,NodeMailer无法发送电子邮件,因为它的SMTP端口(默认值为25个)无处不在,因为它不是主机的端口,而是Docker容器拥有网络的端口。您必须将容器的端口25曝光,并将其映射到主机的端口25或直接在主机网络上运行容器。最好的Solutino是使用- add-host = host.docker.internal:host-gateway flag,容器的localhost so localhost指向实际docker主机的本地主机。

当尝试使用usefetch < /code>将API调用到 /服务器目录中的路由时,我

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

会遇到错误,但开发工作正常,但是当我将其部署到服务器时,它会失败。这是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 

我需要在NUXT配置中设置baseurl吗?但是,没有提及在文档中需要这样做的。这些网络错误对我来说的问题是,我真的不知道在哪里寻找问题。也许它与我设置为应用程序的NGINX代理经理有关?

如果它与此有关,这是我的NUXT配置(启用SSR):

import { defineNuxtConfig } from 'nuxt'

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

Edit: As Hiran Chaudhuri mentioned this is not a Nuxt related issue. I use Nodemailer on my server to send out an email when the user fills out a contact form. However Nodemailer can't send out the email, because the SMTP port for it (25 is default) points to nowhere as it is not the port of the host but of the docker containers own network. You have to expose port 25 of the container and map it to port 25 of the host or run the container on the host network directly. The best solutino though is to use the --add-host=host.docker.internal:host-gateway flag so localhost of the container points to localhost of the actual docker host.

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,
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2025-02-12 15:21:51

连接Econnrefuse 127.0.0.1:25

似乎您正在尝试连接到Localhost上运行的某些SMTP服务。
在Docker中,每个容器都以其自己的IP地址为专用主机运行。这意味着除了您的容器代码外,此主机上没有其他内容,因此错误消息是正确的。

为了解决这个问题,您可以

  • 在主机网络上运行容器
  • 指定邮件服务器的真实IP地址

进一步阅读: https:https:https:https:https:https: //docs.docker.com/network/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文