API introduction

How to create a node.js project

image-20231116111404558

In the terminal:

npm install express --save

Exercises 1

In the following exercises you can create a new script for each exercise

A)

Create a basic API that will output "Hello World" when a user sends a request to the API.

B)

Create a basic API that will generate a random numberarrow-up-right between 1-20 and output the number to the user.

C)

Create a basic API that will generate a random numberarrow-up-right between 1-4 and output "spring", "summer","fall","winter" each according to the number 1-4.

D)

Create a basic API that will generate 10 random numbersarrow-up-right between 1-20 and and output the average to the user.

E)

Using the getday functionarrow-up-right build a simple API that will provide the user with a string output of what day it is today

E.2) (Advanced - optional)

Change the API such that the API outputs YES if it is friday and NO if it is not friday. Like the service www.erdetfredag.dk

Exercises 2

  • Create a .json file with the data underneath and use it as a data-foundation (meaning the .json file represents a database)

  • Each exercise should be written as an end-point

Exercises 2

A) /Hello

  • Create an end point that displays a friendly greeting to the user of the API

B) /first

  • Create an end point that displays the first json object of the data array (data[])

**C) /products **

  • Create an endpoint that displays a list of all products.

D) /products/:id

E) /products/unavailable

  • Create an endpoint that displays all out of stock products

F) products/category/:category

  • Create an endpoint that displays all products from a single category.

G) products/:id/availability

  • Create an endpoint that displays the availability of a product by its ID

Advanced (Optional)

H) products/:min/:max

  • Create an endpoint that displays all products within the minimum - maximum price range

I) products/sortprice

  • Create an endpoint that displays all products of a category sorted by price

J) products/material/unique

  • Create an endpoint that displays a list of all unique material

Data

Last updated