我需要创建一个Soap服务器,但仅用于测试目的。 我的肥皂客户端使用真正的肥皂服务,但不是我的。 index.js和server.js位于同一文件夹级别。
的wsdl:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://corpwsdl.oneninetwo" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://corpwsdl.oneninetwo" xmlns:intf="http://corpwsdl.oneninetwo" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://rpc.xml.coldfusion" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<schema targetNamespace="http://rpc.xml.coldfusion" xmlns="http://www.w3.org/2001/XMLSchema">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<complexType name="CFCInvocationException">
<sequence/>
</complexType>
</schema>
</wsdl:types>
<wsdl:message name="CFCInvocationException">
<wsdl:part name="fault" type="tns1:CFCInvocationException"/>
</wsdl:message>
<wsdl:message name="searchResponse">
<wsdl:part name="searchReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="searchRequest">
<wsdl:part name="xml" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="IDSearch">
<wsdl:operation name="search" parameterOrder="xml">
<wsdl:input message="impl:searchRequest" name="searchRequest"/>
<wsdl:output message="impl:searchResponse" name="searchResponse"/>
<wsdl:fault message="impl:CFCInvocationException" name="CFCInvocationException"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="IDSearch.cfcSoapBinding" type="impl:IDSearch">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="search">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="searchRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://corpwsdl.oneninetwo" use="encoded"/>
</wsdl:input>
<wsdl:output name="searchResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://corpwsdl.oneninetwo" use="encoded"/>
</wsdl:output>
<wsdl:fault name="CFCInvocationException">
<wsdlsoap:fault encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" name="CFCInvocationException" namespace="http://corpwsdl.oneninetwo" use="encoded"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="IDSearch">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
Main ID Search Web Service </wsdl:documentation>
<wsdl:port binding="impl:IDSearch.cfcSoapBinding" name="IDSearch.cfc">
<wsdlsoap:address location="http://localhost:8001/exp?wsdl"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
与客户的索引:
var url = 'http://localhost:8001/exp';
var options = {
disableCache: true,
escapeXML: true,
envelopeKey: 'SOAP-ENV'
};
var args = {
xml: '<![CDATA['+xml+']]>'
//xml : '<test>hello</test>'
};
soap.createClient(url, options, function (err, client) {
client.search(args, function (err, result, body) {
if (err) {
console.log("error: ", err);
console.log("Last request: \n", client.lastRequest + "\n");
}
var parsingOptions = {
'object': true,
'sanitize': false
};
console.log("Response \n\n", body);
//var jsonResult = parser.toJson(body, parsingOptions);
//console.log(jsonResult['soapenv:Envelope']['soapenv:Body']['ns1:searchResponse']['searchReturn']['$t']);
});
});
server.js
var soap = require('soap');
var http = require('http');
var fs = require('fs');
var express = require('express');
var bp = require('body-parser');
var xmlParser = require('xml');
var app = express();
////app.use(bp.json());
console.log("Starting 192com SOAP service");
var myService = {
IDSearch : { // wsdl:service
IDSearch : { // wsdl:port
search: function (args) {
return {
status: "good"
};
}
}
}
}
var xml = require('fs').readFileSync('test/192com/192com.wsdl', 'utf8');
//body parser middleware are supported (optional)
app.use(bp.raw({type: function(){return true;}, limit: '20mb'}));
app.listen(8001, function(){
//Note: /wsdl route will be handled by soap module
//and all other routes & middleware will continue to work
soap.listen(app, '/wsdl', myService, xml);
});
app.get('/exp', function (req, res) {
res.set('Content-Type', 'text/xml');
res.send(xml);
});
当我去localhost:8001 / exp我得到WSDL
当我执行soap客户端时,我得到了这个回复:
error: { Fault:
{ faultcode: 500,
faultstring: 'Invalid XML',
detail: 'Error: Unexpected close tag\nLine: 5\nColumn: 7\nChar: >',
statusCode: 500 },
response:
回复
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /exp</pre>
</body>
所以任何人都有想法我的server.js或wsdl有什么问题?
答案 0 :(得分:2)
您的肥皂服务器需要收听&#39; / exp&#39;路线,不是&#39; / wsdl&#39;路线,你的快递应用程序不会处理&#39; / exp&#39;使用app.get()进行路由。
然后soap服务器将处理路由&#39; / exp?wsdl&#39;自动提供wsdl信息作为对客户端获取请求的响应。 之后,您的客户端将发布请求,并将根据xml文件中的配置和提供给soap.listen函数的服务进行处理。