Fundamentals of node JS
Understanding REST architecture
REST stands for Representational State Transfer. It is just a logical way of building api making them easy to consume.
Principles of setting up logical APIs
Separate APIs into logical resources. All the data we want to share to the API should be divided into logical resources.
Expose structured, resource-based URL.
Use HTTP methods.
Send JSON data in response.
API must be stateless.
CRUD Operations
Create - POST method
Read - GET Method
Update - PUT Method
Delete - Delete Method
Working with Express JS
Creating an API using the REST architecture
Lets create a new json folder called data
and inside the data
folder lets create a movies.json
file and create an array with movie objects. ππΌ
Using the Get()
method, we want to return the movies.json
data as the response in the browser. To achieve this we meed to read the file using the fs()
module and then create an endpoint as the root URL. In order to read the file only once, we use the readFileSync()
method and assign the value to a variable. But the we want to convert the json file to a JavaScript file and to achieve this we use the JSON.parse()
method which was covered in one of our previous lessons. Before sending the response we use the enveloping method to wrap the data inside another object. It simply means we formatted the data using the JSON JSON
formattingππΌ
Lets run the file and see the response.ππΌ
We want to create a count property to display the number of movies we have on the list. Lets update the code base and see the response. ππΌ
Lets restart the server and see the response ππΌ
The count shows that we have 3 movies on the list.
That's all for this lesson.
Next: Handling Post Requests
See you soonππ«‘βπΌ...