首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Node.js:如何使用SOAP XML web服务

Node.js:如何使用SOAP XML web服务
EN

Stack Overflow用户
提问于 2011-12-28 19:22:16
回答 11查看 217.1K关注 0票数 107

我想知道在node.js中使用SOAP XML web服务的最佳方式是什么。

谢谢!

EN

回答 11

Stack Overflow用户

发布于 2017-01-25 04:02:48

我设法使用soap,wsdl和Node.js你需要用npm install soap安装soap

创建一个名为server.js的节点服务器,它将定义要由远程客户端使用的soap服务。此soap服务根据体重(Kg)和身高(M)计算身体质量指数。

代码语言:javascript
复制
const soap = require('soap');
const express = require('express');
const app = express();
/**
 * this is remote service defined in this file, that can be accessed by clients, who will supply args
 * response is returned to the calling client
 * our service calculates bmi by dividing weight in kilograms by square of height in metres
 */
const service = {
  BMI_Service: {
    BMI_Port: {
      calculateBMI(args) {
        //console.log(Date().getFullYear())
        const year = new Date().getFullYear();
        const n = args.weight / (args.height * args.height);
        console.log(n);
        return { bmi: n };
      }
    }
  }
};
// xml data is extracted from wsdl file created
const xml = require('fs').readFileSync('./bmicalculator.wsdl', 'utf8');
//create an express server and pass it to a soap server
const server = app.listen(3030, function() {
  const host = '127.0.0.1';
  const port = server.address().port;
});
soap.listen(server, '/bmicalculator', service, xml);

接下来,创建将使用由server.js定义的soap服务的client.js文件。此文件将为soap服务提供参数,并使用SOAP的服务端口和端点调用url。

代码语言:javascript
复制
const express = require('express');
const soap = require('soap');
const url = 'http://localhost:3030/bmicalculator?wsdl';
const args = { weight: 65.7, height: 1.63 };
soap.createClient(url, function(err, client) {
  if (err) console.error(err);
  else {
    client.calculateBMI(args, function(err, response) {
      if (err) console.error(err);
      else {
        console.log(response);
        res.send(response);
      }
    });
  }
});

wsdl文件是基于xml的数据交换协议,用于定义如何访问远程web服务。将wsdl文件命名为bmicalculator.wsdl

代码语言:javascript
复制
<definitions name="HelloService" targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl" 
  xmlns="http://schemas.xmlsoap.org/wsdl/" 
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
  xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <message name="getBMIRequest">
    <part name="weight" type="xsd:float"/>
    <part name="height" type="xsd:float"/>
  </message>

  <message name="getBMIResponse">
    <part name="bmi" type="xsd:float"/>
  </message>

  <portType name="Hello_PortType">
    <operation name="calculateBMI">
      <input message="tns:getBMIRequest"/>
      <output message="tns:getBMIResponse"/>
    </operation>
  </portType>

  <binding name="Hello_Binding" type="tns:Hello_PortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="calculateBMI">
      <soap:operation soapAction="calculateBMI"/>
      <input>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/>
      </input>
      <output>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/>
      </output>
    </operation>
  </binding>

  <service name="BMI_Service">
    <documentation>WSDL File for HelloService</documentation>
    <port binding="tns:Hello_Binding" name="BMI_Port">
      <soap:address location="http://localhost:3030/bmicalculator/" />
    </port>
  </service>
</definitions>

希望能有所帮助

票数 20
EN

Stack Overflow用户

发布于 2016-05-21 02:57:26

我发现使用Node.js将原始XML发送到SOAP服务的最简单方法是使用Node.js http实现。它看起来像这样。

代码语言:javascript
复制
var http = require('http');
var http_options = {
  hostname: 'localhost',
  port: 80,
  path: '/LocationOfSOAPServer/',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': xml.length
  }
}

var req = http.request(http_options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });

  res.on('end', () => {
    console.log('No more data in response.')
  })
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(xml); // xml would have been set somewhere to a complete xml document in the form of a string
req.end();

您可以将xml变量定义为字符串形式的原始xml。

但是,如果您只想通过Node.js与SOAP服务交互并进行常规的SOAP调用,而不是发送原始的xml,那么可以使用Node.js库之一。我喜欢node-soap

票数 14
EN

Stack Overflow用户

发布于 2015-04-18 00:11:20

我在10多个跟踪WebApis上成功地使用了"soap“包(https://www.npmjs.com/package/soap) (Tradetracker,Bbelboon,Affilinet,Webgains,...)。

问题通常来自这样一个事实,即程序员不会过多地调查远程API需要什么才能进行连接或身份验证。

例如,PHP自动从HTTP头重新发送cookie,但当使用'node‘包时,它必须显式设置(例如通过'soap-cookie’包)...

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8655252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档