RESTful APIs with NodeJS. An Introductory Course for Prospective DBooks Contributors
Repository
https://github.com/feekayo/dbooks-api
What will I learn
- You will learn what REST architectural style is
- You will learn the intricacies of RESTful APIs and what makes them RESTful
- You will learn the HTTP verbs and their usage
- You will learn HTTP Status codes and their meanings
- You will learn how to name resources and interact with said resource
- You will see how to create a simple endpoint on the DBooks API using NodeJS
Requirements
- Dbooks-api
- NodeJS
- ExpressJS
- Basic knowledge of NodeJS
- Any Suitable code editor
Difficulty
- Intermediate
Tutorial Content
This is the first tutorial in a series aimed at providing would be Dbooks.org Contributors with a means of jumping right in to the application source code and providing new functionality without too much ado.
In this tutorial we would be covering the concept of RESTful APIs, what they entail, their methods, rules and inner workings; We would round everything up by setting up a basic RESTful endpoint with NodeJS on the dbooks-api repository. This endpoint would be a RESTful resource that we can be use to communicate with client devices (web, desktop and mobile).
How to Setup Dbooks-api
Setting up the dbooks-api on your local device has been covered in the Readme.md of the Dbooks-api repository. But let do a brief recap
- Download or clone the dbooks api repository
- From the dbooks-api root directory, run npm install
- After installation. Run node app.js
Now Let's get started
What are RESTful web services
REST stands for Representational State Transfer, which according to Wikipedia is an architectural style that defines a set of constraints to be used for creating Web Services.
Web services that meet these constraints are called RESTful web services and they provide interoperability between computer systems on the internet.
This simply means that regardless of the states or contexts of execution of client devices, they would all share the same and equal ability to enjoy these web resources.
There are six constraints a web Service must meet to be considered RESTful, these include
- Client-Server Architecture :- There must be a separation of concerns between the client and the server. This helps separate concerns like user interfacing from data storage and improves the ability to implement user interfaces across multiple platforms
- Uniform interface:- All clients, regardless of platform type or context of executions must have access to the same interface. A good example of this is the ability of Android and IOS implementations of the same applications to share the same web services
- Statelessness:- No client context is stored on the server between requests, and a session state is saved by the client. The RESTful resource has no idea what a request entails till it allows it through.
- Cacheability:- Responses from RESTful services are must be cacheable, in cases where some of these responses are repetitive
- Layered System:- This allows for scalability, RESTful web services must be allowed the ability to possess intermediary servers which enable load balancing, shared caches and even enforce security policies
- Code on demand (optional):- Servers can temporarily allow functionality of a client to be extended by transferring executable code to the client. This means that from the server, you could tweak certain functionality of the Client.
Quick Tips for RESTful APIs
1. Use HTTP verbs to give your requests meaning
API consumers use HTTP verbs to understand the context of operation of API resources. These consumers/clients send requests with HTTP verbs, these HTTP verbs include
GET
Generally used to read a specific resource by a specific identifier or a collection of resources
PUT
Generally used to update a specific resource by a specific resource identifier
DELETE
Generally used to delete a specific resource by a specific resource identifier
POST
Generally used to create a new resource. They are also mostly used for all other operations that don’t fit into the other categories
2. Provide Sensible Resource Names
Let your resource names be easily readable and understandable to even the most untrained eyes. This makes it easy for your APIs to be easily understandable by someone who is not familiar with your API and its inner workings
Here are some quick rules for creating resource paths (URL paths)
- Use identifiers in urls rather than in the query string
- Leverage hierarchical nature of the URL to imply structure
- Design for your clients not for your data
- Resource names should be nouns not verbs
- Use plurals in URL segments to keep your API URLs consistent across all HTTP methods
- Use lowercase in UR segments, separating words with underscores or hyphens
- Keep URLs as short as possible, with as few segments as makes sense
3. Use HTTP response codes to Indicate Status
HTTP Response status codes are used to communicate server statuses post execution. There are quite a number of status codes. They include
- 200 OK:- General success status code. Most commonly used code, used to indicate success
- 201 Created:- Used to indicate success in creating via PUT or POST
- 204 No Content:- Indicate success, but with nothing in response body
- 400 Bad Request:- General Error
- 401 Unauthorized:- Invalid authentication token
- 403 Forbidden:- Forbidden request
- 405 Method Not Allowed:- requested URL exists, but you’re not allowed access to it
- 409 Conflict:- Caused when a request in a resource conflict
- 500 Internal Server Error:- General Error
Thanks for following the tutorial. Questions would be entertained in the comment section
Curriculum
N/A
Thank you for your contribution.
This moderation might not be considered due to the below:
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Congratulations! Your post has been selected as a daily Steemit truffle! It is listed on rank 10 of all contributions awarded today. You can find the TOP DAILY TRUFFLE PICKS HERE.
I upvoted your contribution because to my mind your post is at least 20 SBD worth and should receive 92 votes. It's now up to the lovely Steemit community to make this come true.
I am
TrufflePig
, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, you can find an explanation here!Have a nice day and sincerely yours,
TrufflePig