【问题标题】:NodeJS receive data from a form, edit and send againNodeJS 从表单接收数据,编辑并再次发送
【发布时间】:2014-03-29 00:38:57
【问题描述】:

基本上我从带有函数事件的表单中接收值:

 var formData="";
 req.on('data', function (chunk){
     formData += chunk;
 });

然后我在另一个 http.request 中再次发送这些值:

 req.on('end', function (){          
  var options = { port: conf.port, path: req.path, 
                  host: conf.host, method: req.method , headers: req.headers};
  authReq = http.request(options, 
            function (response){...});

  authReq.write(formData);
  authReq.end();
});

我想要的是将更多数据添加到从以前的表单接收的表单中,例如这样的:

var formData="client_id=XXX&client_secret=XXX&";
req.on('data', function (chunk){
    formData += chunk;
});

但这不起作用,在制作 authReq.write(formData); 之后看起来数据发送是空的或错误的......有什么建议吗?

【问题讨论】:

  • 如果你 a) 正确缩进代码,没有人愿意水平滚动,你会得到更好的反馈; b)您将您的问题减少到最小版本,这样我们就不必通过无关的和特定于应用程序的代码
  • 问题是我无法将另一个字符串连接到 fromData 字符串,该字符串包含表单中的数据...

标签: node.js express oauth oauth-2.0


【解决方案1】:

在第二个请求中,第一个请求中填写的变量formData将为空。 您可能需要使用会话。 (http://expressjs-book.com/forums/topic/express-js-sessions-a-detailed-tutorial/)

另外,从您的代码 sn-ps 的外观来看,我建议您查看 http://passportjs.org/ 进行身份验证:)

【讨论】:

  • 我正在使用 OAuth2,我需要在服务器端将我的 client_secret 和 client_id 添加到请求中,然后再将请求从 nodejs 发送到 oauth 服务器
  • 问题是请求中的标头,而不是使用默认提供的标头,然后使用简单的值重写。 var options = { port: conf.port, path: req.path, host: conf.host, method: req.method , headers: {'Content-Type':'application/x-www-form-urlencoded'} } ; :)
猜你喜欢
  • 2019-07-29
  • 2021-07-21
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多