Skip to content

MykeShale/Express-JS-Boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Open in Visual Studio Code

Express.js Product API

A RESTful API built with Express.js that implements CRUD operations for products, with features like authentication, logging, and error handling.

🚀 Features

  • RESTful API endpoints for product management
  • Authentication using API keys
  • Request logging
  • Error handling
  • Product filtering and pagination
  • Search functionality
  • Product statistics

📋 Prerequisites

  • Node.js (v18 or higher)
  • npm (Node Package Manager)

🔧 Installation

  1. Clone the repository:
git clone <repository-url>
cd <repository-name>
  1. Install dependencies:
npm install
  1. Create a .env file based on .env.example:
cp .env.example .env
  1. Update the .env file with your configuration:
PORT=3000
API_KEY=your_api_key_here

🚀 Running the Server

Start the server:

npm start

The server will start on http://localhost:3000 (or the port specified in your .env file)

📚 API Documentation

Authentication

All product endpoints require an API key to be sent in the request headers:

X-API-Key: your_api_key_here

Endpoints

Products

  • GET /api/products - Get all products

    • Query parameters:
      • category: Filter by category
      • page: Page number (default: 1)
      • limit: Items per page (default: 10)
    • Example: GET /api/products?category=electronics&page=1&limit=10
  • GET /api/products/:id - Get a specific product

    • Example: GET /api/products/1
  • POST /api/products - Create a new product

    • Body:
      {
        "name": "Product Name",
        "description": "Product Description",
        "price": 99.99,
        "category": "category",
        "inStock": true
      }
  • PUT /api/products/:id - Update a product

    • Example: PUT /api/products/1
    • Body: Same as POST
  • DELETE /api/products/:id - Delete a product

    • Example: DELETE /api/products/1

Search and Statistics

  • GET /api/products/search?q=query - Search products by name

    • Example: GET /api/products/search?q=laptop
  • GET /api/products/stats/categories - Get product count by category

🧪 Testing

You can test the API using tools like Postman, Insomnia, or curl. Here's an example using curl:

# Get all products
curl -H "X-API-Key: your_api_key_here" http://localhost:3000/api/products

# Create a new product
curl -X POST -H "Content-Type: application/json" -H "X-API-Key: your_api_key_here" \
  -d '{"name":"New Product","description":"Description","price":99.99,"category":"electronics","inStock":true}' \
  http://localhost:3000/api/products

📝 Error Handling

The API uses standard HTTP status codes and returns error messages in JSON format:

{
  "error": "Error message here"
}

Common status codes:

  • 200: Success
  • 201: Created
  • 400: Bad Request
  • 401: Unauthorized
  • 403: Forbidden
  • 404: Not Found
  • 500: Internal Server Error

Files Included

  • Week2-Assignment.md: Detailed assignment instructions
  • server.js: Starter Express.js server file
  • .env.example: Example environment variables file

Submission

Your work will be automatically submitted when you push to your GitHub Classroom repository. Make sure to:

  1. Complete all the required API endpoints
  2. Implement the middleware and error handling
  3. Document your API in the README.md
  4. Include examples of requests and responses

Resources

About

A starter Express.js project providing a robust foundation for building RESTful APIs or web applications. Includes essential middleware, routing structure, and error handling, making it easy to customize and scale for various backend development needs. Ideal for rapid Node.js app prototyping.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors