当我在 Postman 中使用表单数据时,结果未定义

Result is undefined when I use form-data in Postman

我可以使用 postman 插件中的 x-www-form-urlencoded 选项卡获取结果,但如果我想从 chrome.

var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
var http = require('http').Server(app);
var bodyParser = require('body-parser');
var Random = require("random-js")();

app.use(bodyParser());
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 

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

  var SecurityToken=req.body.SecurityToken;
  var SessionID=req.body.SessionID;
  var TimeStamp=Date.now();
  var SecretTransactionKey=req.body.SecretTransactionKey;
  var TransactionID=req.body.TransactionID;
  var BanksTransactionRefID=req.body.BanksTransactionRefID;
  var SessionRequestType=req.body.SessionRequestType;
  var StatusCode=req.body.StatusCode;
  var StatusDescription=req.body.StatusDescription;
  var tag=req.body.tag;

  var dataFile={"SecurityToken":SecurityToken,"SessionID":SessionID,"TimeStamp":TimeStamp,
  "SecretTransactionKey":SecretTransactionKey,"TransactionID":TransactionID,"BanksTransactionRefID":BanksTransactionRefID,
  "SessionRequestType":SessionRequestType,"StatusCode":StatusCode,"StatusDescription":StatusDescription,
  "Tag":tag};

  res.send('TimeStamp : '+dataFile.TimeStamp+'\nTransactionID : '+dataFile.TransactionID+'\nReplyId : 12993784\nStatusCode : '+dataFile.StatusCode+'\nStatusDescription : '+dataFile.StatusDescription+'\nTags :'+dataFile.Tag);

});

 function checkStatusCode(){
  var status=["Confirmed","Failed","Pending"];
  return status[Random.integer(0,2)];
 }
app.listen(port);
console.log('Server started! At http://localhost:' + port);

您正在使用的代码解析 application/x-www-form-urlencoded,而发布的内容是 multipart/form-data 通过 postman

中的表单数据选项卡

使用强大的 https://www.npmjs.com/package/formidable

添加这个

var formidable = require('formidable');
var util = require('util');
app.post('/TransactionDelay', function(req, res) {
    var form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
        res.writeHead(200, {
            'content-type': 'text/plain'
        });
        res.write('received upload:\n\n');
        res.end(util.inspect({
            fields: fields,
            files: files
        }));
    });
}

这用于文件上传或者你有很多参数,在你的情况下它没有用

简而言之,只需使用 x-www-form-urlencoded 选项卡 :D