简体   繁体   English

如何将POST正文数据传递到另一条路径而不丢失数据?

[英]How to pass POST body data to another route without losing the data?

Let us assume that i have this route 让我们假设我有这条路线

 app.post('/category',function(req,res){
    res.redirect('/category/item');
 })

The user is going to post some data in this route and then is going to be redirected automatically to another route for an example /category/item to post another data 用户将在此路线中发布一些数据,然后将自动重定向到另一个路线,以便示例/category/item发布另一个数据

So at the end i want to collect those two post's data the one in route /category and the other one in route /category/item to query my database. 所以最后我想收集路线/category那两个帖子的数据,以及路由/category/item的另一个来查询我的数据库。

Using express session is the best choice to save the first posted data and then get those data in the second request, here is an example: 使用快速会话是保存第一个发布数据然后在第二个请求中获取这些数据的最佳选择,这是一个示例:

add session middleware 添加会话中间件

const express = require('express'),
    app = express(),
    session = require('express-session');

// use session middleware
app.use(session({
    secret: 'secret',
    resave: false,
    saveUninitialized: true,
    cookie: { maxAge: 60000 }
}));

Store data in session 将数据存储在会话中

app.post('/category', function (req, res) {
    // store posted data in the session
    req.session.category = req.body;

    res.redirect('/category/item');
});

app.post('/category/item', function (req, res) {

    // force users to deal with /category first
    if ( !req.session.category ) {
        return res.redirect('/category');
    }

    // get data from the session
    var category = req.session.category;

    // req.body contains the second submited data
});

You can do this using http status code 307 which indicates that the request should be repeated using the same method and post data. 您可以使用http状态代码307来执行此操作,该状态代码307指示应使用相同的方法重复请求并发布数据。

app.post('/', function(req, res) {
  res.redirect(307, '/category/item');
});

Which will preserve the send data. 这将保留发送数据。

For reference, the 307 http code spec is: 作为参考,307 http代码规范是:

307 Temporary Redirect (since HTTP/1.1) In this occasion, the request should be repeated with another URI, but future requests can still use the original URI.2 In contrast to 303, the request method should not be changed when reissuing the original request. 307临时重定向(自HTTP / 1.1起)在这种情况下,请求应该使用另一个URI重复,但将来的请求仍然可以使用原始URI.2与303相比,重新发出原始请求时不应更改请求方法。 For instance, a POST request must be repeated using another POST request. 例如,必须使用另一个POST请求重复POST请求。

You'll need to store the data from the first call either in memory (a variable) or in your database. 您需要将第一次调用的数据存储在内存(变量)或数据库中。 HTTP is a completely stateless protocol, so there will be no way to communicate across that boundary (one request to another). HTTP是一种完全无状态的协议,因此无法跨越该边界进行通信(一个请求到另一个边界)。 I'd recommend storing the data in your DB, perhaps in a temporary hold table, then syncing it up after the second request is made. 我建议将数据存储在数据库中,可能存放在临时保存表中,然后在第二次请求后进行同步。

app.post('/category',function(req, res){
  // store data from req.body in a temporary table in your DB
  // be sure to key it with something that will tie it to future requests
  // such as a userId, sessionId, etc

  res.redirect('/category/item');
});

app.post('/category/item',function(req, res){
  // find existing data from the temporary DB table using some key
  // like the userId, sessionId, etc

  // then add take the new data from req.body, merge it with the data 
  // from the temporary table above, and store it in the final 
  // location in the DB
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM