【问题标题】:NodeJS - post data is cached in the request bodyNodeJS - 发布数据缓存在请求正文中
【发布时间】:2014-08-01 03:40:40
【问题描述】:

我有两个节点服务器,其中节点服务器 2(端口 3001)接收来自节点服务器 1(在端口 3000 上运行)的请求。

当我在 server1 中绑定数据并对 server2 进行 post 调用时,server2 总是缓存在先前调用中维护的旧数据。

   For ex : Old Post data is     { emailid : 'xxxx',
                          name : 'XXXX',
                          records : [0,1,2,3,4]
                          } 

      If Next POST data from server1 is       { emailid : 'xxxx',
                                            name : 'XXXX',
                                            records : [5,6]
                                          } 
      Then Server2 recieves these data as      { emailid : 'xxxx',
                                            name : 'XXXX',
                                            records : [5,6,2,3,4]
                                          } 

     I dont want the old records 2,3,4 in req.body.records array .. but it is cached. 

     Could you please help me how to remove the caching of the old data in request body container.


    Please look at my server1 code where i make REST calls using node request module: 

Server1 配置 -

app.configure(function() {
    // The cookieParser should be above session
     app.use(express.cookieParser());
    app.use(express.session({secret:'ayer'}));

    // Request body parsing middleware should be above methodOverride
    app.use(express.urlencoded());
    app.use(express.json());
 //      app.use(expressValidator());
    app.use(express.methodOverride());

    // Routes should be at the last
    app.use(app.router);

// Setting the fav icon and static folder
//app.use(express.favicon());
app.use(express.favicon(__dirname + '/public/favicon.ico'));
//app.use('/', express.static(config.root + '/public'));

 });

对 server2 进行 REST API 调用的方法 export.docleansematch = function(req,res,sessionStore){

/*setting up user verification request options*/
var Options = {
    uri: 'http:localhost:3001/docleansematch',
    method: 'POST',
    json:true,
    proxy: config.app.proxy, // calling to internal server
    headers: {'Content-Type':'application/x-www-form-urlencoded'
              },
    form : req.body

};
request.post(Options,function(err,extResponse,extResponseBody)
{
    var data = {};
    if(err)
    {
        log.error('Error in App layer : ',err);
        data.status = 2;
    }
    else
    {
        data.status = extResponseBody.status;

    }
    res.jsonp(data);

});

};

Server2 接收请求数据的方法:

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


    log.info('Cached Number of records - '+ req.body.records.length);


    res.json({status:3}); 
}

【问题讨论】:

  • 请发一个真实的测试用例结果。在您的示例中,第二个回复中没有 0,1。它不加起来。还发布一个可重现的示例。我们可以在我们的机器上测试的东西。

标签: node.js express mean-stack


【解决方案1】:

在我的进一步分析中,Server1 在发送请求表单提交中的值时维护了一个缓存..

var Options = {
uri: 'http:localhost:3001/docleansematch',
method: 'POST',
json:true,
proxy: config.app.proxy, // calling to internal server
headers: {'Content-Type':'application/x-www-form-urlencoded'
          },
form : req.body

 };
 request.post(Options,function(err,extResponse,extResponseBody)
{
var data = {};
if(err)
{
    log.error('Error in App layer : ',err);
    data.status = 2;
}
else
{
    data.status = extResponseBody.status;

}
res.jsonp(data);

});

};

Server1 -> form : req.body 缓存在 post 请求中发送的值,并且在 subsequest post 调用中将旧数据附加到 form.. 以便 server2 接收所有附加数据。

你知道如何清除nodejs的request模块中的缓存吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    相关资源
    最近更新 更多