Here’s a Node.js service that listens for requests on port 3000 and returns a “Hello World” message:
Let’s compare that to the same service built with Express:
While you certainly do not need Express to write Web services, it does offer several benefits, including more succinct code, HTTP utility methods and middleware, improved performance, to name a few.
Not everyone agrees on what distinguishes a web service from regular HTTP requests, APIs, Service Oriented Architecture, etc., but, for the purposes of this series, let’s define a web service as any piece of software that makes itself available over the Internet using a standardized data transfer protocol such as XML or JSON to encode all communications between it and clients. You could even use plaintext for very simple services, but, in most cases, employing a standardized data transfer protocol will allow your service to communicate with clients on any operating system as well as built using any programming language. Hence, a Java service can communicate with an Angular client and a Windows server can respond to requests from Unix applications. There are some other criteria such as being discoverable via a simple find mechanism, but that is beyond the scope of this series.
A RESTful Web Service is one that is built on the REST architecture. REST stands for “REpresentational State Transfer”. It’s been around ever since 2004, when the web consortium released the definition of the RESTful standard. Since that time, the REST standard has become quite popular and is now being used by many of the most popular websites around the world including Facebook and Twitter.
There are other types of Web services as well, the other main one being SOAP. Short for “Simple Object Access Protocol, it’s a very old web services access protocol that was originally developed by Microsoft. It’s considered to be a bit of a dinosaur nowadays and is rarely encountered any more. Having had the opportunity to work with SOAP a few years back, I can see why REST eclipsed it.
There are few piece that you’ll need in place in order to follow along with this series:
Here’s what mine looks like:
We can also use npm to install dependencies like Express. Here’s the command to do that:
The -save flag tells npm to add express as a dependency in our package.json file.
All we need now is a server.
And that’s all there is to it. We’ve got ourselves a new server!
To run your app, issue the node app.js command in the VS Code terminal. You should see our console.log() message displayed:
In part one of this series on RESTful Web services we set up a basic Web service using Node.js and the Express framework. You can see the fruits of our labors in the demo below:
The original home of HTML tutorials
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.