CNode

Ws.js:基于 Node.js的WS-*实现

Yyingjie0904发布于12 年前最后回复12 年前6 回复12572 浏览0 收藏

###简介 Node.js是优秀的用于搭建可扩展服务器应用程序的平台,其中的一些应用程序需要与已存在的网络服务进行交互。只要这些服务是基于Rest,就不会成为问题—因为Rest服务在node世界里是最高级公民。如果需要使用一个soap网络服务, google一下node-soap,或者自己动手做一个soap信封。真正的挑战是当node需要使用soap服务时,它用的是WS-*标准(WS-安全标准,MTOM等等)。几个月前,当我面对这一情况时,没能找到任何模块帮忙。这就是我决定建Ws.js的原因。

2,写代码

var ws = require('ws.js')
  , Http = ws.Http
  , Security = ws.Security
  , UsernameToken = ws.UsernameToken

var request =  '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">' +
                  '<Header />' +
                    '<Body>' +
                      '<EchoString xmlns="http://tempuri.org/">' +
                        '<s>123</s>' +
                      '</EchoString>' +
                    '</Body>' +
                '</Envelope>'

var ctx =  { request: request 
           , url: "http://service/security"
           , action: "http://tempuri.org/EchoString"
           , contentType: "text/xml" 
           }

var handlers =  [ new Security({}, [new UsernameToken({username: "yaron", password: "1234"})])
                , new Http()
                ]

ws.send(handlers, ctx, function(ctx) {                    
  console.log("response: " + ctx.response);
})  

var ctx = { request: request , url: "http://service/security" , action: "http://tempuri.org/EchoString" , contentType: "text/xml" }


<br />
之后的几行是ws-*的核心。我们在请求里定义自己想要使用的协议,这个特定的请求使用ws-安全标准,并对其进行配置来发送一个象征的用户名。

var handlers = [ new Security({}, [new UsernameToken({username: "yaron", password: "1234"})]) , new Http() ]

<br />
最后,这条代码发送请求(使用规定的协议)并对回复进行处理。

ws.send(handlers, ctx, function(ctx) {
console.log("response: " + ctx.response); })


最后的soap是这个样子的:

发送MTOM附件的过程似曾相识,只需要指定我们想要发送的文件,还有它对应的指向soap元素的路径:

//add attachment to the soap request
ws.addAttachment(ctx, "request", "//*[local-name(.)='File1']", 
                "me.jpg", "image/jpeg")
var handlers =  [ new Mtom()
                , new Http()
                ];   

整个示例在这儿。

//actual logic here...

this.next.send(ctx, function(ctx) { self.receive(ctx, callback) }) }

SecurityHandler.prototype.receive = function(ctx, callback) {

//optionally post processing here...

callback(ctx) }

var s = new SecurityHandler() s.next = new HttpHandler() s.send(ctx, function(ctx) {...})


<br />
像很多node app那样,Ws.js同样使用一些外部模块,特别是依靠强大的xml处理库。正如我在[这儿](http://webservices20.blogspot.com/2012/03/xml-stack-for-nodejs-that-works-on.html)提到的,要发现windows上基于node.js xml parser的dom不那么容易,最终我找到了[xmldom](https://github.com/jindw/xmldom)和[xpath.js](http://xpath.js/)。

其他Ws.js使用的有名的库是[node-formidable](https://github.com/felixge/node-formidable)和[node-bufferjs](https://github.com/coolaj86/node-bufferjs),对于mime解析会有帮助。

<br />
###Ws.js的未来
Ws.js框架正处于成长阶段,未来的版本,我计划添加更多高级安全标准,如x.509数字签名和加密。如果你有特别请求,发邮件到我的博客。如果想提供帮助,尽管在[github](https://github.com/yaronn/ws.js)加入进来—这样,Ws.js会发展很快。

<br /><br />
By Yaron Naveh
From:http://www.codeproject.com/Articles/373317/Ws-js-A-Ws-implementation-for-Node-js
查看回复

回复 (6)

A
alsotang#1·12 年前

我以为是这个:https://www.npmjs.org/package/ws

P
Pana#2·12 年前

厉害

S
struCoder#3·12 年前

awesome!本以为SOA中的WS-* Protocols在WCF中通过WSHttpBinding的实现,在node中很难实现,今天,长见识了。学习学习···

Y
yingjie0904#4·12 年前
引用 struCoderawesome!本以为SOA中的WS Protocols在WCF中通过WSHttpBinding的实现,在node中很难实现,今天,长见识了。学习学习···

ws.js还没有做到像WCF那样全面,但微软对node.js的关注很多,估计很多该有的东西都会有的。

R
racyily#6·12 年前

用过ws

参与回复
登录后即可参与回复。登录