1 var http = require(
'http')
2 , https = require(
'https')
4 , EventEmitter = require(
'events').EventEmitter
5 , Serializer = require(
'./serializer')
6 , Deserializer = require(
'./deserializer')
23 function Server(options, isSecure, onListening) {
25 if (
false === (
this instanceof Server)) {
26 return new Server(options, isSecure)
28 onListening = onListening ||
function() {}
32 if (typeof options ===
'string') {
33 options = url.parse(options)
34 options.host = options.hostname
35 options.path = options.pathname
38 function handleMethodCall(request, response) {
39 var deserializer =
new Deserializer()
40 deserializer.deserializeMethodCall(request, function(error, methodName, params) {
41 if (Object.prototype.hasOwnProperty.call(that._events, methodName)) {
42 that.emit(methodName, null, params,
function(error, value) {
45 xml = Serializer.serializeFault(error)
48 xml = Serializer.serializeMethodResponse(value)
50 response.writeHead(200, {
'Content-Type':
'text/xml'})
55 that.emit(
'NotFound', methodName, params)
56 response.writeHead(404)
62 this.httpServer = isSecure ? https.createServer(options, handleMethodCall)
63 : http.createServer(handleMethodCall)
65 process.nextTick(
function() {
66 this.httpServer.listen(options.port, options.host, onListening)
68 this.close =
function(callback) {
69 this.httpServer.once(
'close', callback)
70 this.httpServer.close()
75 Server.prototype.__proto__ = EventEmitter.prototype
77 module.exports = Server