Contenido del Curso
Node.js Express: API & CLI Apps
Node.js Express: API & CLI Apps
Starting and Testing the API
Now that we've completed the development of our Twitter-like API, it's time to run the application and test its functionality. To start the app, open your terminal and run the following command:
Once you see the success message in the terminal, you can open Postman to observe how our app responds to client requests.
Note
If you ever find yourself stuck or want to dive deeper into the code, you can access the complete source code of this Twitter-like API on our GitHub repository.
Testing in Postman
Let's analyze the URLs responsible for different functionalities and see how the API responds to each request.
Get All Posts
Use this request to retrieve all posts from our database. No request body or extra parameters are needed.
- Method: GET;
- URI:
localhost:3000/api/
; - Response:
Get a Post by its ID
Retrieve a specific post based on its ID. You should pass the ID into the URL; the request body remains unchanged.
- Method: GET;
- URI:
localhost:3000/api/post/2
; - Response:
Create a Post
Create a new post by providing valid data to the API. The data must be in JSON format and contain the correct fields.
- Method: POST;
- URI:
localhost:3000/api/
; - Request Body:
- Response:
Update a Post
Update an existing post by providing the post's ID in the parameters and valid data in the request body in JSON format. The API will update the database accordingly
- Method: PUT;
- URI:
localhost:3000/api/post/3
; - Request Body:
- Response:
Delete a Post
Delete a post from the database by passing the post ID in the URL parameters.
- Method: DELETE;
- URI:
localhost:3000/api/post/1
; - Response:
By following these steps and testing the API using Postman, you can ensure that it functions as expected, handling various requests and providing appropriate responses.
¡Gracias por tus comentarios!