Javascript 节点在请求中的数据处理完成后响应
我希望根据我收到的已完成的请求做出不同的响应。我正在发送POST请求并收到一个xml文件。结果要么是成功,要么是错误。我使用Javascript 节点在请求中的数据处理完成后响应,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我希望根据我收到的已完成的请求做出不同的响应。我正在发送POST请求并收到一个xml文件。结果要么是成功,要么是错误。我使用xml2json将xml转换为json对象,然后根据要输出json的响应 问题是我不能在响应中包含响应。我也无法保存回调的值以供以后使用(因为它是异步的) 我曾考虑过使用承诺,但我不确定。我该怎么办 操作顺序应为: 1) 发送请求 2) 获取缓冲区响应 3) 加入缓冲区。将xml处理为JSON 4) 根据JSON条目的类型,如果xml响应为错误,则输出res.JSON('s
xml2json
将xml转换为json对象,然后根据要输出json的响应
问题是我不能在响应中包含响应。我也无法保存回调的值以供以后使用(因为它是异步的)
我曾考虑过使用承诺,但我不确定。我该怎么办
操作顺序应为:
1) 发送请求
2) 获取缓冲区响应
3) 加入缓冲区。将xml处理为JSON
4) 根据JSON条目的类型,如果xml响应为错误,则输出res.JSON('success')
或res.JSON('error')
app.post('/api/submit', (req, res) => {
...
const request = https.request(options, (res) => {
let chunks = [];
res.on("data", function(chunk) {
chunks.push(chunk);
});
res.on("end", function(err) {
if (err) throw err;
let body = Buffer.concat(chunks);
xmlConverter(body, function(err, result) {
console.dir(result);
if (result.entry) {
console.log('✅ Success')
//Respond with json here --> res.json('success')
} else if (result.error) {
console.log('There was an error processing your request');
//or here if there was an error --> res.json('error')
}
});
});
});
request.end()
不要对两个res使用相同的名称,因为它们是不同的变量。只需使用out res变量以所需的值响应请求。 我想应该是这样的:
app.post('/
api/submit', (req, res) => {
...
const request = https.request(options, (resValue) => {
let chunks = [];
resValue.on("data", function(chunk) {
chunks.push(chunk);
});
resValue.on("end", function(err) {
if (err) throw err;
let body = Buffer.concat(chunks);
xmlConverter(body, function(err, result) {
console.dir(result);
if (result.entry) {
console.log('✅ Success')
res.json('success')
} else if (result.error) {
console.log('There was an error processing your request');
res.json('error')
}
});
});
});
request.end()
您可以在回调中进行响应。问题是您有两个变量,都名为
res
,因此一个变量与另一个变量之间存在阴影。您只需更改res
变量名中的一个,这样您就不会对其进行隐藏。例如,您可以更改:
const request = https.request(options, (http_res) // <--change argument name
然后,您只需在获得数据时调用它:
if (result.entry) {
console.log('✅ Success')
http_res.json('success') // <-- use the response object from request
process_data(result)
if(结果项){
console.log('✅ 成功’)
http_res.json('success')/到底是什么问题?您完全可以重命名提供给https.request(options,callbackFunction)的回调函数的参数——该变量的名称并不重要
app.post('/api/submit', (req, res) => {
const request = https.request(options, (potato) => {
let chunks = [];
potato.on("data", function(chunk) {
chunks.push(chunk);
});
potato.on("end", function(err) {
if (err) throw err; // TODO res.status(500).json({}); ??
let body = Buffer.concat(chunks);
xmlConverter(body, function(err, result) {
console.dir(result);
if (result.entry) {
console.log('✅ Success')
res.status(200).json({});
} else if (result.error) {
console.log('There was an error processing your request');
res.status(500).json({});
}
request.end()
});
});
});
});
if (result.entry) {
console.log('✅ Success')
http_res.json('success') // <-- use the response object from request
process_data(result)
app.post('/api/submit', (req, res) => {
const request = https.request(options, (potato) => {
let chunks = [];
potato.on("data", function(chunk) {
chunks.push(chunk);
});
potato.on("end", function(err) {
if (err) throw err; // TODO res.status(500).json({}); ??
let body = Buffer.concat(chunks);
xmlConverter(body, function(err, result) {
console.dir(result);
if (result.entry) {
console.log('✅ Success')
res.status(200).json({});
} else if (result.error) {
console.log('There was an error processing your request');
res.status(500).json({});
}
request.end()
});
});
});
});