可以请求到mockapi的数据,但是请求后台登录接口,报错404
来源:4-3 用户登录后台实现
慕九州2549101
2023-03-30
不知道是不是proxy代理未生效还是其他地方有bug,对着老师的代码检查了好久也没发现哪错了,能不能拜托老师帮我调试一下
前端仓库:https://github.com/fuyachen/admin-front
后端仓库:https://github.com/fuyachen/admin-server
vite.config:
export default defineConfig({
plugins: [vue()],
server: {
proxy: {
"/api": {
//将以api为前缀的请求代理到target
target: "http://localhost:3000",
changeOrigin: true,
},
},
},
})
src/config/index.js:
const env = import.meta.env.MODE || "production" //获取当前环境模式,如果没有则默认开发模式
const EnvConfig = {
development: {
baseApi: "/api",
//真实接口地址
mockApi:
"https://console-mock.apipost.cn/mock/befc349c-0be5-46ad-8304-440fc3f8665b",
//模拟接口的地址
},
...
}
export default {
env,
mock: false,
namedSpace: "manager",
...EnvConfig[env], //根据key:env取对应环境下的配置
}
后端 routes/users
...
router.prefix("/users")
router.post("/login", async (ctx) => {
try {
const { userName, userPwd } = ctx.request.body
const res = await User.findOne({
userName, //当key和value同名,简写
userPwd,
})
if (res) {
ctx.body = util.success(res)
} else {
ctx.body = util.fail("用户名或密码错误")
}
} catch (error) {
// ctx.body = util.fail(error)
console.log(error)
}
})
app.js
// routes
// 一级路由,为后端路由加上统一的前缀/api,便于跟前端路由区分
router.prefix = "/api"
router.use(users.routes(), users.allowedMethods()) //二级路由
app.use(router.routes(), router.allowedMethods()) //注册router
写回答
1回答
-
app.js中的prefix是方法,不能直接赋值,在课程群已经帮你解决。
112023-04-03
相似问题