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: ""
}
}
})