相关疑难解决方法(0)

Expressjs生体

如何访问expressjs给我的请求对象的原始主体?

var express = require('./node_modules/express');
var app = express.createServer();
app.post('/', function(req, res)
{
    console.log(req.body); //says 'undefined'
});
app.listen(80);
Run Code Online (Sandbox Code Playgroud)

node.js express

50
推荐指数
10
解决办法
6万
查看次数

BodyParser:意外令牌 -

我实际上正在开发一个带有文件上传和一些数据的 Angular 网络表单。以下是请求标头:

POST /tests/add HTTP/1.1
Host: localhost:3000
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
Referer: http://localhost:8000/
Content-Length: 2033
Run Code Online (Sandbox Code Playgroud)

我以这种方式构建请求:

var formData = new FormData();
formData.append('file', $scope.test.file);
$http({
  method: 'POST',
  url: backUrl + '/tests/add',
  data: { 'file': $scope.file,
          'token': 'test'},
  contentType: false,
  processData: false,
  transformRequest: function (data, headersGetter) {
      var formData = new FormData();
      angular.forEach(data, function (value, key) {
          formData.append(key, value);
      });
      var headers = headersGetter();
      delete headers['Content-Type'];
      return formData;
  }
Run Code Online (Sandbox Code Playgroud)

但它总是返回一个带有此错误的 400 错误请求:

Unexpected token -
400 …
Run Code Online (Sandbox Code Playgroud)

node.js express angularjs

6
推荐指数
1
解决办法
8022
查看次数

将 CSV 文件从浏览器发送到 nodejs 服务器

我正在尝试将用户上传的 csv 文件从浏览器发送到 nodejs 服务器进行处理(文件超过 50 mb,因此页面变得无响应)。为此,我正在使用 XMLHttpRequest。我找不到解决方案。任何帮助表示赞赏。

Javascript代码

 var csv = document.getElementById('inputFile').files[0];
 var request = new XMLHttpRequest();
 request.open("POST", "/handleFile", true);
 request.setRequestHeader("Content-type", "text/csv");
 request.onreadystatechange = function() {
   if (request.readyState === XMLHttpRequest.DONE && request.status === 200) {
     console.log("yey");
   }
 }

 request.send(csv);
Run Code Online (Sandbox Code Playgroud)

节点服务器

 var express = require('express')
 var app = express()
 var bodyparser = require('body-parser')

 app.post('/handleFile', function(req, res) {
   console.log(req.body); // getting {} empty object here....
   console.log(req);

   var csv = req.body;
   var lines = csv.split("\n");
   var result = [];
   var headers = …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

6
推荐指数
1
解决办法
1万
查看次数

如何在bodyparser之前访问请求的原始主体?

我正在编写一个自定义中间件,它为每个请求生成加密签名(它与AWS API v4使用的身份验证机制非常相似).为了正确生成此签名,我必须获取HTTP请求的整个原始主体.

我也在使用BodyParser,它是我的自定义中间件之后注册的.

我的自定义中间件可以表示如下:

// libs/simplifiedSignatureCheckerMiddleware.js

module.exports = function (req, res, next){

// simple and fast hashing stuff

var payload = '';
req.on('data', function(chunk) { payload += chunk }, null);
req.on('end', function(){

    // hmac stuff

    console.log(payload);

    var ok = true; // ...

    if(ok)
        next();
    else
        next("Bad")
});

}
Run Code Online (Sandbox Code Playgroud)

这就是我在服务器上使用它的方式.

// simpleServer.js

// BASE SETUP
// =============================================================================
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var jsonStream = require('express-jsonstream');
var …
Run Code Online (Sandbox Code Playgroud)

node.js express

5
推荐指数
1
解决办法
5402
查看次数

Nodejs - Expressjs - 验证 shopify webhook

我正在尝试验证hmacshopify webhook开发环境中发送的代码。但是shopify不会向webhook非实时端点发送对 a 的 post 请求,因此我使用requestbin来捕获请求,然后将postman其发送到我的本地网络服务器。

从 shopify文档来看,我似乎一切都做得对,并且还尝试应用node-shopify-auth verifyWebhookHMAC 函数中使用的方法。但到目前为止,这些都没有奏效。代码永远不会匹配。我在这里做错了什么?

我验证网络钩子的代码:

 function verifyWebHook(req, res, next) {
      var message = JSON.stringify(req.body);
    //Shopify seems to be escaping forward slashes when the build the HMAC
        // so we need to do the same otherwise it will fail validation
        // Shopify also seems to replace '&' with \u0026 ...
        //message = message.replace('/', '\\/');
        message = message.split('/').join('\\/');
    message …
Run Code Online (Sandbox Code Playgroud)

hmac webhooks node.js shopify

5
推荐指数
1
解决办法
3400
查看次数

Node.js请求库 - 将text/xml发布到body?

我正在尝试设置一个简单的node.js代理来将帖子传递给Web服务(CSW in this isntance).

我在请求体中发布XML,并指定text/xml. - 服务需要这些.

我在req.rawBody var中得到原始的xml文本并且它工作正常,但我似乎无法正确地重新提交它.

我的方法看起来像:

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


  console.log("Making request to:"  + geobusOptions.host + "With query params: " + req.rawBody);


request.post(
    {url:'http://192.168.0.100/csw',
    body : req.rawBody,
    'Content-Type': 'text/xml'
    },
    function (error, response, body) {        
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);
});
Run Code Online (Sandbox Code Playgroud)

我只想在POST中使用content-type text/xml提交一个字符串.但我似乎无法实现这一目标!

我正在使用'request'库@ https://github.com/mikeal/request

编辑 - 哎呀!我忘了添加标题...

这非常有效:

request.post(
    {url:'http://192.168.0.100/csw',
    body : req.rawBody,
    headers: {'Content-Type': 'text/xml'}
    },
    function (error, response, body) {        
        if (!error && response.statusCode == 200) {
            console.log(body) …
Run Code Online (Sandbox Code Playgroud)

request node.js express

4
推荐指数
1
解决办法
3万
查看次数

用于在请求对象中保存原始发布数据的中间件不会"下一步"并导致超时

我需要获取node/express应用程序中特定端点的原始发布数据.我做:

app.use('/paypal/ipn',function(req, res, next) {
    var postData='';

    req.on('data', function(chunk) { 
        postData += chunk;
    });

    req.on('end', function() {
        req.rawBody = postData;
        console.log('ended buffering. result: ' + req.rawBody);
        next();
    });
});
Run Code Online (Sandbox Code Playgroud)

会发生什么是我在控制台中获得console.log输出,然后没有任何反应.大约一分钟后,我看到服务器返回200 - 可能是超时.这就像next()命令永远不会执行,或执行和stales.

当我注释掉所有内容时,只需调用next():

app.use('/paypal/ipn',function(req, res, next) {
    /*
    var postData='';

    req.on('data', function(chunk) { 
        postData += chunk;
    });

    req.on('end', function() {
        req.rawBody = postData;
        console.log('ended buffering. result: ' + req.rawBody);
        next();
    });
    */
    next();
});
Run Code Online (Sandbox Code Playgroud)

一切正常,即调用端点(当然请求不包含rawBody).

所以我似乎在缓冲rawBody的方式做错了什么?导致next()不起作用的东西?

post node.js express

4
推荐指数
1
解决办法
4163
查看次数

验证 Facebook X-Hub-签名

在 Parse 上的 Cloud Code 上,我尝试验证从 Facebook webhook 收到的标头x-hub-signature

secret是 Facebook 应用程序的正确密钥。

var
hmac,
expectedSignature,
payload = JSON.stringify(req.body),
secret = 'xyzxyzxyz';

hmac = crypto.createHmac('sha1', secret);
hmac.update(payload, 'utf-8');
expectedSignature = 'sha1=' + hmac.digest('hex');
console.log(expectedSignature);
console.log(req.headers['x-hub-signature']);
Run Code Online (Sandbox Code Playgroud)

但签名永远不匹配。怎么了?

javascript facebook webhooks node.js parse-platform

3
推荐指数
1
解决办法
4529
查看次数