364 CHAPTER 8 Websites and services
Lesson 2: Working with web services
In the previous lesson, you saw how you can create a website that can serve webpages
and provide HTML responses to requests. One problem with the website scenario is that
posting form data causes a response that repaints the page with the HTML result. If the
HTML response looks like the existing page, you waste bandwidth and resources to repaint
the page.
Web services provide the ability to send data to the server and receive data back from
the server without requiring a repaint of the browser screen. There are various types of web
services, and this lesson helps you understand them. You also use Node.js to create a web
service.
To send requests to a web service from your webpage, you will be using AJAX, which
stands for Asynchronous JavaScript and XML. Using AJAX, you can write JavaScript that asyn-
chronously calls the server and processes the result. While this is happening, the user’s screen
doesn’t lock up or freeze while waiting for the call to the server to complete.
After this lesson, you will be able to:
■■Explain the difference between websites and web services.
■■Understand the differences between web service types.
■■Create a Node.js web service.
■■Use AJAX to call a web service.
Estimated lesson time: 30 minutes
Introducing web services
A web service provides communications between a web server and a client. The client can be
a web browser, another web service, a cell phone, or any machine that can communicate with
the web service. A client needs the ability to assemble a request, send the request to the web
service, and then receive the response and parse it.
As web services become more pervasive, developers can create applications by just piec-
ing together web services. This is known as creating mashups.
There are two major classes of web services. The first is called Representational State
Transfer (REST), in which the primary purpose of the service is to manipulate web resources
by using a uniform set of stateless operations. The second is arbitrary web services, in which
the service might expose an arbitrary set of operations. An example implementation of REST
web service is called WEB API; an example of an arbitrary set of operations is the Microsoft
Windows Communication Foundation (WCF).
Key
Te rms