Koa+mongo reports error: Error: connect ECONNREFUSED 127.0.0.1 after receiving post request

problem description

after the post request is sent by the current end, the database successfully writes the data, but the backend still reports an error of Error: connect ECONNREFUSED 127.0.0.1, and returns 500.

the environmental background of the problems and what methods you have tried

the backend is built with koa, and mongoose operates the database. I checked that the default returned port is 80, but 80 is inaccessible, so I added a proxy

to the app.js of koa.
var proxy = require("koa-better-http-proxy")

app.use(
  proxy("/register", { target: "http://127.0.0.1:3000", changeOrigin: true })
)

related codes

//User.js
router.post("/register", async ctx => {
  const { username, password, email } = ctx.request.body
  let user = User.find({
    username
  })
  if (user.length) {
    ctx.body = {
      code: -1,
      msg: ""
    }
  }

  let newUser = await User.create({
    username,
    password,
    email
  })
  if (newUser) {
    let res = await axios.post("/login", {
      username,
      password
    })

    if (res.data && res.data.code === 0) {
      ctx.body = {
        code: 0,
        msg: "",
        user: res.data.user
      }
    } else {
      ctx.body = {
        code: -1,
        msg: "error"
      }
    }
  } else {
    ctx.body = {
      code: -1,
      mgs: ""
    }
  }
})

I found that there is a problem with authority authentication after the jump login. In the koa-passport section

router.post('/login', async (ctx, next) => {
  // ctx.body = {
  //   code: 0,
  //   msg: {
  //     ...ctx.request.body
  //   }
  // }
  return passport.authenticate('local', function(err, user, info, status) {
    if (err) {
      ctx.body = {
        code: -1,
        msg: err
      }
    } else {
      //
      if (user) {
        ctx.body = {
          code: 0,
          msg: '',
          user
        }
        return ctx.login(user)
      } else {
        ctx.body = {
          code: 1,
          msg: info
        }
      }
    }
  })(ctx, next)

ctx.login will report an error. The method login cannot be found in ctx

has been resolved, and the middleware is called in the wrong order

app.use(passport.initialize())
app.use(passport.session())

should be placed before router

Menu