2017-03-07 59 views
0

我需要創建一個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

當我執行我得到的肥皂客戶端時這樣的響應:

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?

回答

2

您的soap服務器需要偵聽'/ exp'路由,而不是'/ wsdl'路由,並且您的快速應用無法通過app.get()處理'/ exp'路由。

然後,soap服務器將自動處理路由'/ exp?wsdl',以提供wsdl信息作爲對客戶端獲取請求的響應。 之後你的客戶端會發佈一個請求,它將根據XML文件中的配置以及提供給soap.listen函數的服務來處理。