Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Running and Testing the REST API | Construindo APIs REST com Node.js e Express.js
Desenvolvimento Backend com Node.js e Express.js

Running and Testing the REST API

Deslize para mostrar o menu

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:

node index

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:
Test final API through Postman - get all posts

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:
Test final API through Postman - get a post by id

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:
Test final API through Postman - request body for the creating the post
  • Response:
content

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:
Test final API though Postman - update the post request body
  • Response:
content

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:
content

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.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 10

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 4. Capítulo 10
some-alt