This project is a serverless API hosted on Netlify using Redis Cloud as the database. The API allows for managing a simple counter that can be incremented, reset, or retrieved. It supports CORS for cross-origin requests.
- Getting Started
- API Endpoints
- Environment Variables
- Dependencies
- Error Handling
- Development
- Deployment
- License
-
Clone the repository:
git clone https://github.com/mondalsurojit/Visitor-Counter-API.git
-
Install dependencies:
npm install
-
Set up environment variables (create a
.env
file):REDIS_HOST=your-redis-host REDIS_PORT=your-redis-port REDIS_PASSWORD=your-redis-password
-
Deploy to Netlify by linking your repository and configuring environment variables through the Netlify dashboard.
This endpoint increments the counter by 1 if no query parameters are provided. If the q
parameter is set to reset
, it resets the counter to 0. If the q
parameter is set to any other value, it returns the current count without incrementing.
- Method:
GET
- URL:
https://visitor-counter-api.netlify.app/.netlify/functions/counter/
Parameter | Type | Description |
---|---|---|
q | string | Optional. If reset , the counter is reset to 0. |
-
Status:
200 OK
-
Body: JSON object containing the current count value
{ "value": <current_count> }
This endpoint handles CORS preflight requests.
- Method:
OPTIONS
- URL:
https://visitor-counter-api.netlify.app/.netlify/functions/counter/
- Status:
200 OK
- Body:
"Preflight OK"
Variable | Description |
---|---|
REDIS_HOST |
The hostname of your Redis instance. |
REDIS_PORT |
The port number of your Redis instance. |
REDIS_PASSWORD |
The password for your Redis instance. |
- redis: Redis client for Node.js.
- dotenv: Module to load environment variables from a .env file.
- netlify-cli: Command line tool for Netlify.
- netlify-lambda: Tools for building and deploying serverless functions with Netlify.
- serverless-http: Serverless framework for running Node.js HTTP servers.
In case of an error while interacting with Redis, the API responds with:
- Status:
500 Internal Server Error
- Body:
"Error interacting with Redis"
To run the project locally, follow these steps:
- Ensure Redis Cloud is configured and accessible.
- Ensure the
.env
file is correctly set up with Redis credentials. - Run the serverless function locally using Netlify CLI:
netlify dev
Deploy the API using Netlify by following their serverless function deployment guide.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to modify this documentation to better suit your specific requirements and project structure.