Report this

What is the reason for this report?

Nuxt 3 app returns 405 Method Not Allowed

Posted on April 17, 2023
Sam

By Sam

Hello, I have deployed an app using Digitalocean APP but every POST request is failing 405 Method not allowed while it’s working in local.

as per Nuxt 3 documentation, I have setup environment Variable as NITRO_PRESET=digital-ocean and similar.

/server/api/aut/login.post.ts

export default defineEventHandler(async (event) => {
  const body = await readBody<{ email: string; password: string; rememberMe: boolean }>(event)
  const config = useRuntimeConfig()

  const {
    email,
    password,
    rememberMe
  } = body

  const data = await $fetch(process.env.API_BASE_URL + "/api/token/", {
    method: "POST",
    body: body
  })

  if (data) {
      setCookie(event, 'access', data.access, {
        httpOnly: true,
        path: '/',
        sameSite: 'strict',
        secure: process.env.NODE_ENV === 'production',
        expires: rememberMe ? new Date(Date.now() + config.cookieRememberMeExpires) : new Date(Date.now() + config.cookieAccessExpires),
      })
      setCookie(event, 'refresh', data.refresh, {
        httpOnly: true,
        path: '/',
        sameSite: 'strict',
        secure: process.env.NODE_ENV === 'production',
        expires: rememberMe ? new Date(Date.now() + config.cookieRememberMeExpires) : new Date(Date.now() + config.cookieRefreshExpires),
      })
  }
  return data

})


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

I recently had a similar problem, make sure that you are using https:// rather than http:// when making the requests. Otherwise, if you send a POST request to http:// which then gets redirected to https:// you would most likely hit the problem that you’ve described.

Let me know how it goes.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.