node.js webservice

來源:互聯網
上載者:User

標籤:webservice   node.js   

    由於項目需要,要瞭解和使用webservice服務,在網上查詢了一下,webservice的特點是實現跨平台,但是我需要自己類比一個伺服器端和用戶端,各種找資料~~~~

    個人心得,不對勿噴--------------

    在node.js 中,webservice一下就能查到soap模組,但我最終並沒有使用soap模組,而是soap-server模組構建伺服器。soap中需要自行定義一個wsdl檔案,其實這個檔案就是定義該伺服器提供哪些服務(方法),以及每個方法的輸入格式和輸出格式,我沒有使用這種方式,因此不羅嗦了。soap-server模組構建伺服器,方式很簡單,不需要自己定義wsdl檔案,它會自動產生,模組中有樣本,此處不再舉例。webservice的實質其實就是http伺服器,只不過它會在該http伺服器上定義一個webservice的訪問路徑,不怎麼說的清楚,舉例:

  var soap = require('soap-server');  function MyTestService(){    }  MyTestService.prototype.test1 = function(myArg1){     return myArg1 + '123456';  };  var soapServer = new soap.SoapServer();  var soapService = soapServer.addService('testService', new MyTestService());  soapServer.listen(8888);
說明一下:此處是經過soap-server封裝過,但實質是http服務監聽了8888連接埠,webservice是接收 /testService 的訪問路徑,即當訪問為:http://xxxx:8888/testService 時,服務會轉到webservice中,假如指定的方法是test1,那麼就會執行函數:
MyTestService.prototype.test1 = function(myArg1){     return myArg1 + '123456';  };


以上是服務端程式,現在來說說用戶端如何訪問,webservice服務在node中基本都是使用soap協議訪問,soap的實質是http和xml,path指定成webservice中的路徑,此處為:/testService。和普通的http訪問方式不一樣的地方有兩項:

    1)headers中需要指定SOAPAction:函數名(需要訪問的函數名,此處為test1)

    2)資料格式為xml,並且是soap格式的xml, 如下:

var xml = '<?xml version="1.0" encoding="utf-8"?>'+ '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+ '<soap:Body>'+ '<test1 xmlns="http://localhost:8888/">'+ '<myArg1>"Krime"</myArg1>'+ '</test1>'+ '</soap:Body>'+ '</soap:Envelope>';
然後按照普通的http訪問方式訪問即可,我測試是使用的POST方式。


node.js webservice

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.