https://github.com/pshycodr/code-first-test
This is for Community tests only.
https://github.com/pshycodr/code-first-test
Last synced: 11 months ago
JSON representation
This is for Community tests only.
- Host: GitHub
- URL: https://github.com/pshycodr/code-first-test
- Owner: pshycodr
- Created: 2025-04-08T13:09:34.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-08T13:14:29.000Z (about 1 year ago)
- Last Synced: 2025-08-01T02:25:18.915Z (11 months ago)
- Language: TypeScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π‘ Idea API β RESTful Backend with Hono + Cloudflare Workers
A simple REST API built using the [Hono](https://hono.dev) framework and deployed on Cloudflare Workers. This API allows users to submit and view ideas for an offline event. Data is stored **in-memory** for the duration of the serverβs runtime.
---
## API: https://codefirsttest.aroy262692.workers.dev/ideas
## π API Endpoints
### β POST `/ideas`
Create a new idea.
#### β
Request Body (JSON)
```json
{
"title": "Workshop on Creativity",
"description": "A deep dive into design thinking."
}
```
#### π’ Success Response
- **Status Code:** `201 Created`
- **Response:**
```json
{
"id": 1,
"title": "Workshop on Creativity",
"description": "A deep dive into design thinking."
}
```
#### π΄ Error Response (Invalid Input)
- **Status Code:** `400 Bad Request`
- **Response:**
```json
{
"error": "Title and description are required and must be non-empty strings."
}
```
---
### π₯ GET `/ideas`
Retrieve all submitted ideas.
#### π’ Success Response
```json
[
{
"id": 1,
"title": "Workshop on Creativity",
"description": "A deep dive into design thinking."
},
...
]
```
---
## β Conceptual Questions
### 1. What are the main HTTP methods used in REST APIs, and what is the typical purpose of each?
- **GET**: Retrieves data from the server.
- **POST**: Creates a new resource on the server.
- **PUT**: Updates an existing resource entirely.
- **PATCH**: Partially updates a resource.
- **DELETE**: Removes a resource from the server.
These methods correspond to CRUD (Create, Read, Update, Delete) operations:
- **Create**: POST
- **Read**: GET
- **Update**: PUT/PATCH
- **Delete**: DELETE
---
### 2. Why is input validation important for a backend API?
Input validation is crucial because it:
- Prevents **bad or malformed data** from entering your system.
- Protects against **security threats** like injection attacks.
- Ensures **data consistency**.
- Helps return **meaningful error messages** to clients.
- Prevents server crashes due to unexpected input types or structures.
Without validation, the server could process incorrect data or become vulnerable to malicious attacks.
---
## π How to Run the Server Locally
### π§° Prerequisites
- Node.js
- `npm`
- `wrangler` (Cloudflare's developer CLI)
Install Wrangler globally:
```bash
npm install -g wrangler
```
### βοΈ Setup
1. Clone the repo:
```bash
git clone https://github.com/pshycodr/code-first-test.git
cd idea-api-hono
```
2. Install dependencies:
```bash
npm install
```
3. Start development server locally:
```bash
npm run dev
```
4. Deploy to Cloudflare Workers:
```bash
npm run deploy
```
> [!IMPORTANT]
> Make sure to set up your Cloudflare account and configure `wrangler.toml` with your account details.
---
## π Project Structure
```
idea-api-hono/
β
βββ src/
β βββ index.ts # API logic (Hono app)
βββ wrangler.toml # Cloudflare config
βββ tsconfig.json # TypeScript config
βββ package.json # Dependencies & scripts
βββ README.md # This file
```