Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Running and Testing the REST API | REST-rajapintojen Rakentaminen Node.js:llä ja Express.js:llä
Backend-kehitys Node.js:llä ja Express.js:llä

Running and Testing the REST API

Pyyhkäise näyttääksesi valikon

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.

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 10

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 4. Luku 10
some-alt