node.js - 使用 express 在 axios post 请求后进行重定向

标签 node.js reactjs express redirect axios

在接受来自 Axios 的发布请求后,我无法使重定向正常工作。我确实知道请求正在发送,并且它至少从“/”路由获得了一些响应,因为我的控制台记录了“索引”、“用户已验证”,这是当有人向“/”发出获取请求时应该发生的情况'.问题是页面无法加载。我什至在 google chrome 的网络选项卡中看到 index.js 已加载,但无论我尝试什么,页面都不会改变!这有什么理由吗?

我所做的其他重定向似乎有效。例如,如果用户未登录,索引页面将重新路由到/login。这似乎只是发布请求的问题,我已经在使用和不使用护照身份验证的情况下对其进行了测试(显然更改为您需要登录in 重定向),结果是一样的。所以我不认为护照是导致问题的原因。

你可以引用下面的package.json看看我用的是什么

axios代码:

axios.post('/login', {username: username, password: password})
        /*.then(response => res.redirect('/'))*/
        .then(function (response) {
            console.log(response);
        })
        .catch(function(error) {
            console.log(error);
        })

表达方面:我有控制台日志在测试期间提醒自己

 server.get('/', (req,res) =>{
    console.log("Index");
  if (req.user){
       console.log("user verified");
        res.redirect('/');
        app.render(req,res, '/',req.query);
  } else {
      console.log("user not logged in");
      res.redirect('/login');
  }
})

server.post('/login', passport.authenticate('local'), (req, res, next) => {
    if (req.user) {
        console.log("Logging in");
        res.redirect('/');
  } else {
        console.log("Passwrod Incorrect");
        return res.redirect('/login');
  }
})

包.json

{
  "name": "layout-component",
  "version": "1.0.0",
  "scripts": {
    "dev": "node ./server.js",
    "build": "next build",
    "start": "NODE_ENV=production node ./server.js"
  },
  "dependencies": {
    "@zeit/next-css": "^0.1.5",
    "axios": "^0.18.0",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.2",
    "connect-flash": "^0.1.1",
    "connect-mongo": "^2.0.1",
    "cookie-parser": "^1.4.3",
    "express": "^4.16.3",
    "express-session": "^1.15.6",
    "express-validator": "^5.1.0",
    "file-loader": "^1.1.11",
    "hoist-non-react-statics": "^2.5.0",
    "jsonwebtoken": "^8.2.0",
    "mongodb": "^3.0.5",
    "mongoose": "^5.0.12",
    "next": "^5.1.0",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0",
    "prop-types": "^15.6.1",
    "react": "^16.3.0",
    "react-dom": "^16.3.0",
    "semantic-ui-css": "^2.3.1",
    "semantic-ui-react": "^0.79.0",
    "url-loader": "^1.0.1"
  },
  "license": "ISC"
}

最佳答案

后来我想通了。显然,当您发出 Axios 发布请求时,您不能从服务器进行重定向。至少不是我这样做的方式(使用默认的 Axios 配置)。您需要在客户端进行页面更改。我是这样做的。

这真的让我感到难过,因为我正在使用其他方法从我的重定向路由接收数据,但页面没有加载。

此外,出于某种原因,使用 Next.js 时,passport.js“successRedirect”和“failureRedirect”JSON 似乎不起作用。这就是为什么我按照我的方式编写路由并且没有将它们包含在 passport.authenticate() 函数中的原因。我希望这对某人有所帮助!

我的 Axios 提交功能:

onSubmit = (e) => {
    e.preventDefault()
    const {username, password} = this.state;
    axios.post('/login', {username: username, password: password})
        .then(function (response) {
            if (response.data.redirect == '/') {
                window.location = "/index"
            } else if (response.data.redirect == '/login'){
                window.location = "/login"
            }
        })
        .catch(function(error) {
            window.location = "/login"
        })
}

我的 Express 服务器中的 post 请求

server.post('/login', passport.authenticate('local'), (req, res, next) => {
    if (req.user) {
        var redir = { redirect: "/" };
        return res.json(redir);
  } else {
        var redir = { redirect: '/login'};
        return res.json(redir);
  }
})

关于node.js - 使用 express 在 axios post 请求后进行重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49601795/

相关文章:

javascript - 遇到两个 child 用同一个key报错

reactjs - Prop 类型失败 : Component: prop type `props` is invalid;

javascript - 下面的代码块有什么作用,我需要它吗?

amazon-web-services - 具有健康检查功能的 Express Session 和 Amazon Beanstalk

javascript - 是否可以创建一个具有动态状态的 reducer ?

javascript - "undefined is not a function"使用回调

c - Node.js ZeroMQ 客户端不与 C ZeroMQ 服务器交换 REQ/REP 消息

node.js - docker 尝试连接 mongodb 时出错

express - Webpack 和 Express - 严重依赖警告

javascript - 如何在对象数组中找到具有最长字符长度的对象键