RESTful APIs

In the stateless request-response model of HTTP communication, the client always initiates a request, and the server responds. This article will discuss this in detail in the scope of Node.js.

While building any web-application, it is true that CRUD operations hold the core functionality. CRUD stands for CREATE, READ, UPDATE, DELETE. To efficiently handle these CRUD operations in web applications, an architectural style called REST (Representational State Transfer) is commonly used for designing APIs.

REST (Representational State Transfer)

  • REST is an architectural style for designing networked applications, mainly web services.

  • It uses HTTP methods to perform CRUD operations in a stateless manner.

These methods include GET, POST, PUT, DELETE mainly:

  • POST → CREATE

  • GET → READ

  • PUT → UPDATE

  • DELETE → DELETE

Each respective method performs a particular CRUD operation. When a client sends an HTTP request to the server, the server processes it and sends an appropriate response. Before jumping on to how client responds, let’s explore how does client sends the request:

1. CLIENT → SERVER [REQUEST]

app.get(“/api-endpoint”, (req, res) => {

//code

});

Node.js server along with Express.js(used for REST routing) accept requests from the client in the above pattern where req and res objects play the main communication role.

Whenever a request object is received it mainly contains:

  1. req.method - HTTP method (GET, POST, etc.)

  2. req.url - Full URL path of the request

  3. req.path - Path part of the URL

  4. req.query - Object containing query string / query parameters

    “/api?key1=value1&key2=value2”

    Here key1 and key2 are query strings which contains their respective values storing data send from the client. For sensitive data, it should not be sent through query parameters or route parameters, as they are exposed in the URL and can be logged or cached.

  5. req.params - Object containing route parameters

    “/api/:id1/:id2”

    Here id1 and id2 are route parameters which contains their respective values storing data send from the client. No sensitive data should be send from here. For sensitive data, it should not be sent through query parameters or route parameters, as they are exposed in the URL and can be logged or cached.

  6. req.body - Data sent in the request body (requires middleware)

    This is used to send sensitive data in the form of an object having key value pairs of fields passed on.

  7. req.headers - Object containing request headers

  8. req.cookies - Object containing cookies (requires cookie-parser)

  9. req.ip - Client's IP address

  10. req.protocol - Protocol (http or https)

2. SERVER → CLIENT [RESPONSE]

Node.js server sends response to a request in the form of Response object .

When a server receives a request, it generates a response object containing various methods and properties to send data back to the client, which mainly contains:

  1. res.status(code) - Set HTTP status code

    Status Codes indicates the status/state of a response. Each code has a certain meaning:

    • 200: OK (request succeeded).

    • 201: Created (new resource created).

    • 400: Bad Request (invalid input).

    • 401: Unauthorized (user not authenticated).

    • 404: Not Found (resource not found).

  2. res.send(data) - Send response (text, HTML, JSON, etc.)

  3. res.json(data) - Send JSON response

  4. res.redirect(url) - Redirect to another URL

  5. res.end() - End the response

  6. res.set(field, value) - Set response header

  7. res.cookie(name, value, options) - Set a cookie

  8. res.clearCookie(name) - Clear a cookie

  9. res.format() - Send different responses based on Accept header

  10. res.type(type) - Set the Content-Type header

This was a brief and simple explanation of RESTful APIs Operations. For more such explanations kindly subscribe. Thanks for reading!