简体   繁体   English

发送 NodeJS ExpressJS 响应 HTTP 状态码 200 + 有效载荷

[英]Send NodeJS ExpressJS Response HTTP Status code 200 + payload

I need to send back an acknowledge response back with the following characteristics:我需要发回具有以下特征的确认响应:

  • HTTP Status code 200 HTTP 状态码 200
  • payload (body?)有效载荷(身体?)

My try:我的尝试:

 function handleMessage(req, res){ let otaRequestBuffer = []; req.on('readable', () => { let data; while (data = req.read()) { otaRequestBuffer.push(data.toString()); } }); req.on('end', () => { let otaRequest = otaRequestBuffer.join(''); try { let parser = new DOMParser(); let xmlDoc = parser.parseFromString(otaRequest, "text/xml"); let soapHeader = parseSoapHeader(xmlDoc); const soapAction = req.headers['soapaction'].toString(); const ack = `<?xml version='1.0' encoding='utf-8'?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Header> <htnga:CorrelationID xmlns:htnga="http://htng.org/PWSWG/2007/02/AsyncHeaders">${soapHeader.correlationId}</htnga:CorrelationID> <htnga:RelatesToCorrelationID xmlns:htnga="http://htng.org/PWSWG/2007/02/AsyncHeaders">${soapHeader.correlationId}</htnga:RelatesToCorrelationID> </env:Header> <env:Body> <ns:HTNG_AcknowledgeReceipt xmlns:ns="http://htng.org/2014B"/> </env:Body> </env:Envelope>` res.sendStatus(200); res.write(ack); } catch (error) { console.log(error); } });

But this only sends OK.但这只会发送OK。 How can I send also the payload/body and the HTTPS status code 200.我怎样才能发送有效载荷/正文和 HTTPS 状态代码 200。

Thanks for the help.谢谢您的帮助。

Here a screenshot from the docs from the API provider:这是来自 API 提供程序的文档的屏幕截图:

ACK-示例

By default the status code is 200 for res.send(), but for other status codes, you can do that using默认情况下 res.send() 的状态码为 200,但对于其他状态码,您可以使用

res.status(403).send("You are not authorised to view this")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM