Welcome to the Dynamic Image API, a powerful and flexible service for fetching animal images on demand. This API also includes a sample endpoint for managing user data.
All API endpoints are relative to the following base URL. Please replace https://img-server-theta.vercel.app/
with the live URL of your Vercel deployment.
https://img-server-theta.vercel.app/
If you wish to run a local instance of this server, please follow the steps below.
- Node.js (v18 or higher)
- npm (Node Package Manager)
- A Pexels API Key (available for free from pexels.com/api)
-
Clone the repository:
git clone Dynamic-Image-Server cd Dynamic-Image-Server
-
Install dependencies:
npm install
-
Configure Environment Variables: Create a file named
.env
in the root of the project and add your Pexels API key.PEXELS_API_KEY="goT7jSJYxYoqXLfc0i66CmfxqZ1ty8IeSGEl4qYVvZhDV3NjJT2Ny8ro"
Note: For this to work, you would need to update
routes/animal.js
to useprocess.env.PEXELS_API_KEY
instead of the hardcoded key. -
Run the server:
node man.js
The server will be running at
http://localhost:3000
.
The core feature of this service. It provides a simple way to get images of animals.
Redirects to an image URL of the specified animal. The service first checks a local cache; if a match isn't found, it fetches an image from the Pexels API.
-
Endpoint:
GET /api/animal/:name
-
Description: The
:name
parameter should be the name of an animal (e.g.,tiger
,dog
,cat
). -
Success Response:
- Code:
302 Found
- Content: The request is redirected to the direct URL of the image.
- Code:
-
Error Response:
- Code:
404 Not Found
- Content:
No image found for this
- Code:
-
Example Usage (HTML):
<img src="https://img-server-theta.vercel.app/api/animal/lion" alt="Image of a Lion">
A sample API for managing mock user data.
Retrieves a list of all sample users.
- Endpoint:
GET /api/users
- Success Response:
- Code:
200 OK
- Content:
[ { "id": 1, "name": "Alice" }, { "id": 2, "name": "Bob" } ]
- Code:
Retrieves a single user by their unique ID.
- Endpoint:
GET /api/users/:id
- Success Response:
- Code:
200 OK
- Content:
{ "id": 1, "name": "Alice" }
- Code:
- Error Response:
- Code:
404 Not Found
- Content:
{ "message": "User not found" }
- Code:
Simulates the creation of a new user.
- Endpoint:
POST /api/users
- Request Body:
{ "name": "Charlie" }
- Success Response:
- Code:
201 Created
- Content:
{ "message": "User created", "user": { "name": "Charlie" } }
- Code: