dword-design / nuxt-mail

Adds email sending capability to a Nuxt.js app. Adds a server route, an injected variable, and uses nodemailer to send emails.
Other
239 stars 18 forks source link

404 error in production #123

Closed raphaelbernhart closed 2 years ago

raphaelbernhart commented 2 years ago

Hey there,

I want to send mail via nuxtjs/nuxt-mail. In development both via nuxt dev and nuxt start commands it works perfect. But if I switch to docker/k8s it is not working. It throws an 404 error as if axios is misconfigured.

my techstack:

My nuxt.config.js

axios: {
        // Workaround to avoid enforcing hard-coded localhost:3000: https://github.com/nuxt-community/axios-module/issues/308
        baseURL: '/',
        browserBaseURL: '/'
    },
mail: {
        message: {
            to: 'mail@raphaelbernhart.at',
        },
        smtp: {
            host: 'mail.raphaelbernhart.at',
            port: 465,
            auth: {
                user: process.env.MAIL_USER,
                pass: process.env.MAIL_PASS,
            },
        },
    },

dockerfile

FROM node:14.17.0-alpine as build

RUN apk add git

# create destination directory
RUN mkdir -p /app
WORKDIR /app

# copy the app, note .dockerignore
COPY ./ /app/
RUN npm install
RUN npm run build

FROM node:14.17.0-alpine as production

# Install git
RUN apk add git

RUN mkdir -p /app
WORKDIR /app

COPY --from=build /app/package.json /app/
COPY --from=build /app/.nuxt /app/.nuxt
COPY --from=build /app/nuxt.config.js /app/
COPY --from=build /app/static /app/static
COPY --from=build /app/assets /app/assets

RUN npm install --production

EXPOSE 3000
ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=3000

CMD [ "npm", "start" ]

Bildschirmfoto 2022-04-17 um 15 06 18

Thanks for the help :)

raphaelbernhart commented 2 years ago

I fixed the error with the following steps:

I thought with nuxt build all the env variables aso were built in the .nuxt directory. But as it turns out you have to copy the nuxt.config.js file into the production environment. This is not documented anywhere so it was hard to solve the problem.

Solution:

I changed my dockerfile from:

COPY --from=build /app/package.json /app/
COPY --from=build /app/.nuxt /app/.nuxt
COPY --from=build /app/static /app/static

to:

COPY --from=build /app/.nuxt /app/.nuxt

COPY ./package.json /app/
COPY ./nuxt.config.js /app/
COPY ./static /app/static

This solves the issue :)

dword-design commented 2 years ago

Thanks for noticing :)