node.js - 通过 react 在特快专递中进行重定向

标签 node.js reactjs express

我正在使用 React 及其路由器来重定向我的应用程序。当我在我的服务器 Express 中使用 get 指令时,它工作正常。但是当我发出后指令时,它不会重定向。我必须做什么?这是我的代码:

(我以我期望的方式收到响应,但之后没有任何反应)

lisaApp.post('/signup', (req, res) => {
  const validationResult = validateSignupForm(req.body);
  if (!validationResult.success) {
    return res.status(400).json({
      success: false,
      message: validationResult.message,
      errors: validationResult.errors
    });
  }

  var newClient = req.body
  newClient.clientemailname = newClient.userclient
  newClient.client_id = uuid.v4()

  delete newClient.confirmPassword

  ClientApi.saveClient(newClient, function (err, usr) {
    if (err) return res.status(500).send(err.message)

    console.log('ready to redirect') //Here, the response has a status "0k"
    res.redirect('/')
  });
});

最佳答案

你可以这样编写你的 api:

    lisaApp.post('/signup', (req, res) => {
      const validationResult = validateSignupForm(req.body);
      if (!validationResult.success) {
        return res.status(400).json({
          success: false,
          message: validationResult.message,
          errors: validationResult.errors
        });
      }

      var newClient = req.body
      newClient.clientemailname = newClient.userclient
      newClient.client_id = uuid.v4()

      delete newClient.confirmPassword

      ClientApi.saveClient(newClient, function (err, usr) {
        if (err) return res.status(500).send(err.message)

        console.log('ready to redirect') //Here, the response has a status "0k"
        return res.status(200).json({
            success:true,
            redirectUrl: '/'
        })
      });
    });

在您的 React 文件中,您可以在 POST 处理程序中使用此代码来使用 React-router 进行重定向。

this.history.pushState(null, res.redirectUrl);

关于node.js - 通过 react 在特快专递中进行重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43207966/

相关文章:

r - 使用 R 连接到 socket.io 套接字

node.js - 需要完成 426 升级

xml - NodeJs CMS - 构建 CMS 时将 XML 或 JSON 作为存储格式

javascript - 如何在使用 create-react-app 创建的应用程序中使用具有访问 token 的 API

mysql - 在 MySQL 连接字符串中指定密码

node.js - 发生 Express 错误时 pm2 不会重新启动工作程序

node.js - 如何在 Node :10 container in GitLabCI中执行docker命令

google-chrome - 在 Chrome 中显示 JSX 文件而不是下载它

reactjs - redux-form 中的 InjectedFormProps 是什么?

node.js - Express 4.17 请求体为空