Book Image

RESTful Web API Design with Node.js - Second Edition

By : Valentin Bojinov
Book Image

RESTful Web API Design with Node.js - Second Edition

By: Valentin Bojinov

Overview of this book

In this era of cloud computing, every data provisioning solution is built in a scalable and fail-safe way. Thus, when building RESTful services, the right choice for the underlying platform is vital. Node.js, with its asynchronous, event-driven architecture, is exactly the right choice to build RESTful APIs. This book will help you enrich your development skills to create scalable, server-side, RESTful applications based on the Node.js platform. Starting with the fundamentals of REST, you will understand why RESTful web services are better data provisioning solution than other technologies. You will start setting up a development environment by installing Node.js, Express.js, and other modules. Next, you will write a simple HTTP request handler and create and test Node.js modules using automated tests and mock objects. You will then have to choose the most appropriate data storage type, having options between a key/value or document data store, and also you will implement automated tests for it. This module will evolve chapter by chapter until it turns into a full-fledged and secure Restful service.
Table of Contents (12 chapters)
RESTful Web API Design with Node.js - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Working with WADL


If you are familiar with SOAP web services, you may have heard of the Web Service Definition Language (WSDL). It is an XML description of the interface of the service and defines an endpoint URL for invocation. It is mandatory for a SOAP web service to be described by such a WSDL definition.

Similar to SOAP web services, RESTful services can also make use of a description language, called WADL. WADL stands for Web Application Definition Language. Unlike WSDL for SOAP web services, a WADL description of a RESTful service is optional, that is, consuming the service has nothing to do with its description.

Here is a sample part of a WADL file that describes the GET operation of our balance service:

<application xmlns="http://wadl.dev.java.net/2009/02" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:service="http://localhost:8080/data/balance"> 
<grammer> 
<include href="balance.xsd"/> 
<include href="error.xsd"/> 
</grammer> 
<resources base="http://localhost:8080/data/balance/"> 
<resource path="{date}"> 
<method name="GET"> 
<request> 
<param name="date" type="xsd:string" style="template"/> 
</request> 
<response status="200"> 
<representation mediaType="application/xml" element="service:balance"/> 
<representation mediaType="application/json" /> 
</response> 
<response status="404"> 
<representation mediaType="application/xml" element="service:balance"/> 
</response> 
</method> 

</resource> 
</resources> 
</application>

This extract of a WADL file shows how application-exposing resources are described. Basically, each resource must be a part of an application. The resource provides the URI where it is located with the base attribute, and describes each of its supported HTTP methods in a method. Additionally, an optional doc element can be used at resource and application to provide additional documentation about the service and its operations.

Though WADL is optional, it significantly reduces the efforts of discovering RESTful services.

Taking advantage of the existing infrastructure

The best part of developing and distributing RESTful applications is that the infrastructure needed is already out there waiting restlessly for you. As RESTful applications use the existing web space heavily, you need to do nothing more than follow the REST principles when developing. In addition, there are plenty of libraries available out there for any platform, and I do mean any given platform. This eases development of RESTful applications, so you just need to choose the preferred platform for you and start developing.