An object oriented web service framework inspired by Windows Communication Foundation
The service-model module provides an extensible web service framework for Node. The API is inspired by the Windows Communication Foundation (WCF). This module focuses on the dispatching of service requests and does not handle the underlying transport or encoding. Therefore, service-model must be paired with a server component such as Express or Restify. Also note that you'll need the appropriate middleware for the type of transport you are using, such as the JSON body-parser. API documentation is available here.
service-model can be installed using npm:
$ npm install service-model --save
Defining a Service Contract
We define a service contract,
Calculator, that has two operations,
subtract. The contract is specified
using decorators on the service implementation,
Configuring the DispatcherFactory
Once our service is defined, we add it to a DispatcherFactory. We then add an endpoint to the service, providing the name of the contract, the base address for the endpoint, and a list of endpoint behaviors.
; ; factory.addServiceCalculatorService .addEndpoint"Calculator", "/api/rpc/calculator", ;
In this case, we add the RpcBehavior which indicates that operations on this endpoint will be available through RPC. Operations can also be made available through REST by adding the RestBehavior which will be described later.
The previously configured factory is then used to create a RequestDispatcher which is responsible for handling service requests. In this example we configure an Express web server to delegate all requests with a base path of "/api" to the RequestDispatcher. The ExpressRequestContext comes packaged in the library but a RequestContext can easily be created for other server platforms.
;; ;; app.use"/api*",; app.listen3000;
Service operations can be made available through a REST api by adding decorators to the service implementation methods.
Revisiting the previously defined
CalculatorService we add the WebGet decorator to the
The WebGet decorator defines a UrlTemplate for each method which is used to choose the operation and decode method parameters.
We then create an endpoint with the RestBehavior. The
CalculatorService now has two endpoints, supporting both
REST and RPC.
; factory.addServiceCalculatorService .addEndpoint"Calculator", "/api/rest/calculator", ;
The service operations are now available using HTTP GET requests:
$ curl http://localhost:3000/api/rest/calculator/add/1/2 3
Licensed under the Apache License 2.0. Note that Microsoft released an open source implementation of their .NET Framework as the Mono Project. The components of the Mono Project that relate to this module are licensed under the MIT X11 terms.