Node.js 如何使用Angular和Express以及其他输入解析多部分POST请求?
我可以解析我的多部分数据,但不能解析表单附带的常规输入信息。我上传了一个word文档,我可以很好地解析它,但是我无法从输入的文本类型中获取信息。我将请求发布到服务器,它就到达了那里,除了请求对象非常奇怪,因为Angularjs不支持本地的多部分提交,我不得不将请求作为angular.identity发布。我正在使用busboy connect解析多部分信息,因此我的方法看起来有点像这样:Node.js 如何使用Angular和Express以及其他输入解析多部分POST请求?,node.js,angularjs,express,mean-stack,Node.js,Angularjs,Express,Mean Stack,我可以解析我的多部分数据,但不能解析表单附带的常规输入信息。我上传了一个word文档,我可以很好地解析它,但是我无法从输入的文本类型中获取信息。我将请求发布到服务器,它就到达了那里,除了请求对象非常奇怪,因为Angularjs不支持本地的多部分提交,我不得不将请求作为angular.identity发布。我正在使用busboy connect解析多部分信息,因此我的方法看起来有点像这样: req.pipe(req.busboy); req.busboy.on('file', func
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
//this code parses the file
}
然而,每当我尝试req.name或req.param(name)时,我总是得到未定义或null。有没有办法在bodyParser和busboy之间切换?我已经在底部将请求打印为JSON
干杯
{ _writableState:
{ highWaterMark: 16384,
objectMode: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
sync: true,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
buffer: [] },
writable: true,
domain: null,
_events:
{ unpipe: [Function: onunpipe],
drain: [Function],
error: [Function: onerror],
close: { [Function: g] listener: [Function: onclose] },
finish: { [Function: g] listener: [Function: onfinish] } },
_maxListeners: 10,
_done: false,
_parser:
{ _needDrain: false,
_pause: false,
_cb: undefined,
_nparts: 0,
_boy: [Circular],
parser:
{ _writableState: [Object],
writable: true,
domain: null,
_events: [Object],
_maxListeners: 10,
_bparser: [Object],
_headerFirst: undefined,
_dashes: 0,
_parts: 0,
_finished: false,
_realFinish: false,
_isPreamble: true,
_justMatched: false,
_firstWrite: true,
_inHeader: true,
_part: undefined,
_cb: undefined,
_ignoreData: false,
_partOpts: {},
_pause: false,
_hparser: [Object] } },
opts:
{ headers:
{ host: 'localhost:3000',
connection: 'keep-alive',
'content-length': '44520',
accept: 'application/json, text/plain, */*',
origin: 'http://localhost:3000',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryyUi7ZbB4IU8PXkzm',
referer: 'http://localhost:3000/',
'accept-encoding': 'gzip,deflate,sdch',
'accept-language': 'en-US,en;q=0.8,it;q=0.6,es;q=0.4',
cookie: 'connect.sid=s%3AZpb0Vg74dgR-1iz3-bXPKhKnZyC9UY4U.uPvsAKswVQStlkMkDAv%2F%2BBIVA1rFzPC63xdZT5b7dnk' } } }
您还需要侦听“field”事件以捕获非文件类型。参见示例