axios 我如何修复我的快速后端重定向到后端端口,而不是重定向到前端端口

xjreopfe  于 2022-12-04  发布在  iOS
关注(0)|答案(1)|浏览(186)

在我的前端(vue在端口9999上),axios请求是:

this.$axios.post('/login', {
        username: this.username,
        password: this.password
      }).then((response) => {
        if (response) {
          if (response.data.code === 200) {
            this.$store.commit('login',response.data.result);
            this.$router.push({path: '/'})

在我后端(端口7890上的Express)中,路由是:

app.post('/login',
    passport.authenticate('local', {
        successRedirect: '/loginSuccess',
        failureRedirect: '/loginError',
        failureFlash: true
    })
);

app.get('/loginSuccess', function (req, res) {
    console.log(req.baseUrl);
    res.send(responseToJson(200, req.user, 'login successed!'));
});

app.get('/loginError', function (req, res) {
    res.send(responseToJson(500, null, 'ユーザーまたはパスワードが正しくありません.....'));
});

但是当我登录时,它显示:

GET http://localhost:9999/loginSuccess 404 (Not Found)
dispatchXhrRequest @ xhr.js?b50d:187
xhrAdapter @ xhr.js?b50d:13
dispatchRequest @ dispatchRequest.js?5270:53
request @ Axios.js?0a06:108
Axios.<computed> @ Axios.js?0a06:140
wrap @ bind.js?1d2b:9
login @ Login.vue?7463:46
invokeWithErrorHandling @ vue.runtime.esm.js?2b0e:1863
invoker @ vue.runtime.esm.js?2b0e:2188
original._wrapper @ vue.runtime.esm.js?2b0e:6961
Login.vue?7463:63 Error: Request failed with status code 404
    at createError (createError.js?2d83:16:1)
    at settle (settle.js?467f:17:1)
    at XMLHttpRequest.onloadend (xhr.js?b50d:54:1)

我代理配置是:

devServer: {
      port:9999,
      proxy: {
        '/api': {
          target: 'http://localhost:7890/',
          changeOrigin: true,
          pathRewrite: {
            '^/api': '/'
          }
        }
      }
    },

它后端请求被重定向到前端的端口。我如何改变后端的重定向到后端端口?

lmvvr0a8

lmvvr0a81#

请尝试passportjs链接中提到的以下选项

app.post('/login',
    passport.authenticate('local', {
        failureRedirect: '/loginError',
        failureFlash: true
    }, (req, res) => {
        res.status(200).send({user: req.user});
    })
);

相关问题