This is an example of a local API using Express.js. The API allows manipulating a collection of users by providing operations such as listing all users, getting a user by ID, adding a new user, updating an existing user, and deleting a user.
Make sure you have Node.js installed on your machine.
- Clone the repository or create a new directory for the project.
- Navigate to the project's root directory.
Run the following command to install the necessary dependencies:
npm install
Run the following command to start the API on a local server:
npm start
The server will be started at localhost:3000
.
Returns a list of all users.
- URL:
/users/all
- Method:
GET
- Success response: Status 200 (OK)
- Example response:
[
{
"id": "cd1c78e4-5a3d-4d57-b832-784fb5ce6730",
"name": "John Doe",
"date_created": "2023-05-25T12:34:56.789Z",
"image_link": "https://source.unsplash.com/random/200x200/?person"
},
{
"id": "42cf52da-7a5a-487b-a0de-89474f158ed0",
"name": "Jane Smith",
"date_created": "2023-05-25T12:34:56.789Z",
"image_link": "https://source.unsplash.com/random/200x200/?face"
}
]
Returns a single user based on the provided ID.
- URL:
/users/:id
- Method:
GET
- Path parameter:
id
(string) - The user ID - Success response: Status 200 (OK)
- User not found response: Status 404 (Not Found)
- Example response for found user:
{
"id": "cd1c78e4-5a3d-4d57-b832-784fb5ce6730",
"name": "John Doe",
"date_created": "2023-05-25T12:34:56.789Z",
"image_link": "https://source.unsplash.com/random/200x200/?person"
}
Adds a new user to the collection.
- URL:
/users
- Method:
POST
- Request body: JSON object containing user data (name)
- Success response: Status 200 (OK)
- Example request body:
{
"name": "Emily Brown"
}
- Example success response:
{
"id": "12345678-abcd-efgh-ijkl-1234567890ab",
"name": "Emily Brown",
"date_created": "2023-05-25T12:34:56.789Z",
"image_link": "https://static.vecteezy.com/system/resources/thumbnails/004/511/281/small/default-avatar-photo-placeholder-profile-picture-vector.jpg"
}
Updates the details of an existing user based on the provided ID.
- URL:
/users/:id
- Method:
PATCH
- Path parameter:
id
(string) - The user ID - Request body: JSON object containing the user data to be updated (name)
- Success response: Status 200 (OK)
- User not found response: Status 404 (Not Found)
- Example request body:
{
"name": "New user name"
}
- Example success response:
{
"id": "cd1c78e4-5a3d-4d57-b832-784fb5ce6730",
"name": "New user name",
"date_created": "2023-05-25T12:34:56.789Z",
"image_link": "https://source.unsplash.com/random/200x200/?person"
}
Deletes a user from the collection based on the provided ID.
- URL:
/users/:id
- Method:
DELETE
- Path parameter:
id
(string) - The user ID - Success response: Status 200 (OK)
- User not found response: Status 404 (Not Found)
- Example success response:
{
"message": "User deleted successfully"
}
The API has been configured to allow Cross-Origin Resource Sharing (CORS). The CORS settings include allowing all origins to access the API, allowing credentials, setting allowed headers, and allowed methods. These settings are applied to all API routes.
const cors = require('cors');
const corsOptions = {
origin: '*', // Defines the allowed origin (all domains)
credentials: true, // Allows the use of cookies and authorization headers with HTTPS
allowedHeaders: 'Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale', // Defines the allowed headers
methods: 'GET,PUT,POST,PATCH,DELETE,OPTIONS' // Defines the allowed methods
};
const corsMiddleware = cors(corsOptions);
userRoutes.use(corsMiddleware); // Applies the cors middleware
This allows you to make requests to the API from any origin, using the specified methods and headers.
This project is licensed under the MIT License - see the LICENSE file for details.