Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Running and Testing the REST API | Bygge REST-API-er med Node.js og Express.js
Backend-Utvikling med Node.js og Express.js

Running and Testing the REST API

Sveip for å vise menyen

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.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 10

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 4. Kapittel 10
some-alt