获取从邮递员发送的二进制文件数据?

时间:2018-10-24 09:13:53

标签: node.js postman

我正在尝试从nodejs中的邮递员获取从二进制选项发送的数据,但是当我打印req.body时,它将返回空白对象。 谁能告诉我如何获取这些数据?

enter image description here

代码:

const express = require("express");
const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.text()); // This will parse your file

app.post('/data/UploadLogsToServer', async (req, res) => {
   res.status(200).send(req.body)
});

const server = app.listen(3000, function () {
    console.log('Server listening on port 3000');
});

Foo.txt文件

StoreApiCode=retailgraph&Content={"Type": "INFO ","TimeStamp":"2018-10-10 12:05:16.881","IP":"2001:0:9d38:90d7:804:3589:8a3c:e43a","MacId":"84:7B:EB:3F:65:A4","OS":"Microsoft Windows 10 Home Single Language","Method":"SyncData","LineNo":527,"Message":"SyncData: Billing Software SyncData() called"}
{"Type": "INFO ","TimeStamp":"2018-10-10 12:05:17.060","IP":"2001:0:9d38:90d7:804:3589:8a3c:e43a","MacId":"84:7B:EB:3F:65:A4","OS":"Microsoft Windows 10 Home Single Language","Method":"SyncData","LineNo":38,"Message":"SyncData: Start"}
{"Type": "INFO ","TimeStamp":"2018-10-10 12:05:17.111","IP":"2001:0:9d38:90d7:804:3589:8a3c:e43a","MacId":"84:7B:EB:3F:65:A4","OS":"Microsoft Windows 10 Home Single Language","Method":"SyncData","LineNo":44,"Message":"SyncData: LocalConnectionString = Data Source=PARAM\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;"}

1 个答案:

答案 0 :(得分:1)

在邮递员中,确保标题为Content-Type = application/octet-stream。然后在您的app.post定义中,分块读取数据。例如,下面它返回一个简单的JSON响应,但至少服务器可以看到数据。看来您的版本没有:

    var data = '';
    app.post('/data/UploadLogsToServer',function(req, res) {

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

    req.on('end', function() {
      console.log(data);
    });

    var responseJson = {
      "message": "success!!!"
    };

    return res.status(200).send(responseJson);
  });