An API provides a way for software systems to exchange information. A REST API uses HTTP methods and resource routes as part of an architectural style for designing APIs. This guide introduces the concepts through Express and Node.js.
APIs and JSON
An API defines how one program can request data or actions from another. A frontend can send an HTTP request to a backend and use the response to update what a person sees. JSON (JavaScript Object Notation) is a widely used data format organized as key-value pairs.
Install Express and create a route
Express is a framework for Node.js that helps organize web applications and APIs. Add it to a project from the terminal with npm install express, then create a small server and define a route that responds to an HTTP request. A “Hello, World!” route is a simple way to begin.
Methods and paths
An HTTP method describes the kind of operation requested; a path identifies the resource or endpoint. Common methods include:
- GET to retrieve data.
- POST to submit or create data.
- PUT to replace or update a resource.
- PATCH to update part of a resource.
- DELETE to request removal of a resource.
Requests can carry data in a body or in URL parameters. A body is commonly used for submitted data, while a path or query parameter can identify a particular item or filter.
Test requests with Postman
Postman is a separate application for sending HTTP requests and inspecting responses; it is not a library installed in the project. Choose a method, enter a URL, set any required headers or request data, and inspect the response.
Workshop: outline a users API
GET /users— retrieve the user list.POST /users— submit a new user.GET /users/:id— retrieve a user by ID.PUT /users/:id— update a user by ID.PATCH /users/:id— update part of a user record.DELETE /users/:id— request deletion of a user by ID.
Practice by building and testing each route. The examples introduce API structure; adapt them to your application and review the behavior of your implementation.
For a free financial consultation, contact the team on LINE. You can also scan the QR code to add the team on LINE.
