gpt4 book ai didi

javascript - Nodejs 中的 SOAP 客户端-服务器通信

转载 作者:行者123 更新时间:2023-12-03 02:31:41 25 4
gpt4 key购买 nike

我正在使用 Strongsoap npm 包 here 在 Nodejs 中进行 SOAP 客户端-服务器通信。我正在使用 AQL 查询处理 Arango 数据库。我试图获取结果并将其显示在服务器端口中。

作为第一步,我创建了一个 SOAP 客户端文件和服务器文件,其中包含 npm 包中提供的示例。我不确定我在这里做错了什么?

User.js

// ##soap.listen
var another = require('./authen.js');
var soap = require('strong-soap').soap;
var http = require('http');

var myService = {
MyService: {
MyPort: {
check_username: function(args) {

res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");

db.query(aqlQuery`
LET startVertex = (FOR doc IN spec
FILTER doc.serial_no == '"123456abcde"'
LIMIT 2
RETURN doc
)[0]

FOR v IN 1 ANY startVertex belongs_to
RETURN v.ip`,
{
bindVar1: 'value',
bindVar2: 'value',
}
)
}
}
}
};

var xml = require('fs').readFileSync('myservice.wsdl', 'utf8'),
server = http.createServer(function(request,response) {
response.end("404: Not Found: " + request.url);
});

server.listen(8000);
soap.listen(server, '/wsdl', myService, xml);

然后我创建了客户端文件以及我在下面共享的 wsdl 文件。一旦我运行我的服务器和客户端文件。我收到一个无法通过功能识别的错误。我感觉我做错了什么。或者我做得对吗?

"use strict";

var soap = require('strong-soap').soap;

var url = 'http://192.00.00.000/test/myservice.wsdl';

var requestArgs = {
symbol: 'IBM'
};

var options = {};

soap.createClient(url, options, function(err, client) {

var method = client['check_username'];

method(requestArgs, function(err, result, envelope, soapHeader) {

console.log('Response Envelope: \n' + envelope);
console.log('Result: \n' + JSON.stringify(result));

});
})

错误:

  • var method = client['check_username'];
  • 类型错误:无法读取未定义的属性“check_username”

最佳答案

我认为您在创建客户端对象时遇到错误。我想说你应该检查soap.createClient调用中的err对象,例如

var options = {};

soap.createClient(url, options, function(err, client) {

if (err) {
console.error("An error has occurred creating SOAP client: " , err);
} else {
// Log a description of the services the server offers.
var description = client.describe();
console.log("Client description:" , description);
// Go on and call the method.
var method = client['check_username'];
method(requestArgs, function(err, result, envelope, soapHeader) {
console.log('Response Envelope: \n' + envelope);
console.log('Result: \n' + JSON.stringify(result));
}
}
});

记录 client.description 非常有用,您可以看到处理请求的服务器对象的结构。

此外,客户端是否指向正确的 URL?您正在服务器上监听端口 8000,客户端是否指向此端口?

在服务器端,我会连接记录器,这可以告诉你发生了什么,例如您收到的信封、GET 请求等。

var soapServer = soap.listen(server, '/wsdl', myService, xml);

soapServer.log = function(type, data) {
// type is 'received' or 'replied'
console.log('Type: ' + type + ' data: ' + data);
};

这是一个完整的示例:

服务器

var soap = require('strong-soap').soap;
var http = require('http');

var myService = {
CheckUserName_Service: {
CheckUserName_Port: {
checkUserName: function(args, soapCallback) {
console.log('checkUserName: Entering function..');
db.query(aqlQuery`
LET startVertex = (FOR doc IN spec
FILTER doc.serial_no == '"123456abcde"'
LIMIT 2
RETURN doc
)[0]

FOR v IN 1 ANY startVertex belongs_to
RETURN v.ip`,
{
bindVar1: 'value',
bindVar2: 'value',
}
).then(function(response) {
console.log(`Retrieved documents.`, response._result);
soapCallback(JSON.stringify(response._result));
})
.catch(function(error) {
console.error('Error getting document', error);
soapCallback('Error getting document' + error.message);
});
}
}
}
};


var xml = require('fs').readFileSync('check_username.wsdl', 'utf8');

var server = http.createServer(function(request,response) {
response.end("404: Not Found: " + request.url);
});

var port = 8000;
server.listen(port);

var soapServer = soap.listen(server, '/test', myService, xml);
soapServer.log = function(type, data) {
console.log('Type: ' + type + ' data: ' + data);
};

console.log('SOAP service listening on port ' + port);

客户端

"use strict";

var soap = require('strong-soap').soap;
var url = 'http://localhost:8000/test?wsdl';

var options = { endpoint: 'http://localhost:8000/test'};
var requestArgs = { userName: "TEST_USER" };
soap.createClient(url, options, function(err, client) {
if (err) {
console.error("An error has occurred creating SOAP client: " , err);
} else {
var description = client.describe();
console.log("Client description:" , description);
var method = client.checkUserName;
method(requestArgs, function(err, result, envelope, soapHeader) {
//response envelope
console.log('Response Envelope: \n' + envelope);
//'result' is the response body
console.log('Result: \n' + JSON.stringify(result));
});
}
});

WSDL

<definitions name = "CheckUserNameService"
targetNamespace = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
xmlns = "http://schemas.xmlsoap.org/wsdl/"
xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema">

<message name = "CheckUserNameRequest">
<part name = "userName" type = "xsd:string"/>
</message>
<message name = "CheckUserNameResponse">
<part name = "status" type = "xsd:string"/>
</message>
<portType name = "CheckUserName_PortType">
<operation name = "checkUserName">
<input message = "tns:CheckUserNameRequest"/>
<output message = "tns:CheckUserNameResponse"/>
</operation>
</portType>

<binding name = "CheckUserName_Binding" type = "tns:CheckUserName_PortType">
<soap:binding style = "rpc"
transport = "http://schemas.xmlsoap.org/soap/http"/>
<operation name = "checkUserName">
<soap:operation soapAction = "checkUserName"/>
<input>
<soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
</input>
<output>
<soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
</output>
</operation>
</binding>

<service name = "CheckUserName_Service">
<documentation>WSDL File for CheckUserNameService</documentation>
<port binding = "tns:CheckUserName_Binding" name = "CheckUserName_Port">
<soap:address
location = "http://www.examples.com/CheckUserName/" />
</port>
</service>
</definitions>

WSDL 应该位于与服务器相同的目录中名为“check_username.wsdl”的文件中。

关于javascript - Nodejs 中的 SOAP 客户端-服务器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48708893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com