Skip to content

sharko99/light-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

41 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌟 LightAPI - Lightweight Express Boilerplate

Welcome to LightAPI, the most modular, easy-to-use, and feature-rich Express API template! Whether you're a beginner or an experienced developer, LightAPI provides a solid foundation to kickstart your project.

πŸ“ Change Log

Date format: YYYY-MM-DD

πŸš€ 1.0.3 - 2024-06-05

  • 🐰 Added Bun compatibility (kush-js)

πŸš€ 1.0.2 - 2024-06-04

  • πŸ”§ Added a middleware to disable routes easily: app.get('/register', disabled, (req, res) => { => This route now returns a 403 error

πŸš€ 1.0.1 - 2024-06-04

  • 🐘 Added postgreSQL support. Credits: kush-js
  • πŸ”„ Changed the default port to 5005
  • πŸ”’ Added a field in the .env to hide user id from userHandler token
  • πŸ“œ Added Authorization header in the routes comments examples
  • βœ… Protected routes and MySQL2 functions have been tested and are working
  • πŸ§ͺ Still need a complete test for postgreSQL (Your PR are welcome :))

πŸš€ 1.0.0 - 2024-06-03

  • πŸŽ‰ Initial release

Features

LightAPI comes packed with a variety of powerful features:

  • πŸ”„ Routes handling: Easily define and manage your API routes.
  • πŸ” User authentication with JWT: Secure user authentication out of the box.
  • πŸ’Ύ MySQL2 / Postgres basic functions: Simple and efficient MySQL2 and Postgres integration.
  • 🐘 PostgreSQL support: Switch between MySQL2 and Postgres with ease.
  • πŸ“§ Nodemailer included: Send emails effortlessly with Nodemailer.
  • πŸ”§ Configuration with DotEnv: Manage environment variables with ease.
  • πŸ“ Winston logging: Robust logging for better debugging and monitoring.
  • πŸ“‘ CORS enabled: Cross-Origin Resource Sharing for flexible API usage.
  • 🚫 Rate limiting: Protect your API from abuse with built-in rate limiting.
  • πŸ” Joi validation: Validate incoming requests with Joi.
  • πŸ›‘οΈ Middleware ready: Pre-configured middleware for common tasks.
  • πŸ“¦ Modular structure: Highly modular design for easy customization and extension.
  • πŸ”’ Easy route disabling: Disable routes easily with middleware.
  • 🐰 Bun compatibility: Works with Bun out of the box. Autodetects the runtime runner.
  • πŸš€ Works out of the box!: Get up and running quickly with minimal configuration.

Getting Started

Prerequisites

Make sure you have Node.js installed on your machine.

Installation

  1. Clone the repository:

        git clone https://github.com/yourusername/lightapi.git
        cd lightapi
  2. Install dependencies:

        npm install

    Or with Bun

        bun install
  3. Copy the .env.example file to .env in the root directory and configure your environment variables:

        # Application settings
        PORT=5005
        JWT_SECRET=your_jwt_secret
    
        # Light API settings
        HIDE_USERID=true # Hide user id in the user token
    
        # Database Type <mysql or postgres>
        DBTYPE=mysql
    
        # MySQL2 settings
        DB_HOST=localhost
        DB_USER=root
        DB_PASSWORD=password
        DB_NAME=testdb
    
        # PostGres settings
        PGHOST=localhost
        PGUSER=root
        PGPASSWORD=PASSWORD
        PGNAME=testdb
    
        # NodeMailer settings
        EMAIL_HOST=smtp.example.com
        EMAIL_PORT=587
        EMAIL_SECURE=false
        EMAIL_USER=your_email@example.com
        EMAIL_PASS=your_email_password
        EMAIL_FROM='Your Name <your_email@example.com>'

Database Tables Setup

For your comfort, here are two queries that you can use on your database to create a LightAPI compatible users table:

Mysql:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Postgres:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- Trigger and function to update the updated_at field automatically
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

Usage

  1. Start the server:
        node app.js
    with Bun:
        bun run app.js

Your API will be running on http://localhost:5005.

Project Structure

    lightapi/
    β”œβ”€β”€ app.js
    β”œβ”€β”€ logger.js
    β”œβ”€β”€ classes/
    β”‚   β”œβ”€β”€ htmlProcessor.js
    β”‚   β”œβ”€β”€ mailer.js
    β”‚   └── userHandler.js
    β”œβ”€β”€ db/
    β”‚   β”œβ”€β”€ postgres.js
    β”‚   β”œβ”€β”€ mysql.js
    β”‚   └── db.js
    β”œβ”€β”€ routes/
    β”‚   β”œβ”€β”€ index.js
    β”‚   β”œβ”€β”€ log.js
    β”‚   β”œβ”€β”€ user.js
    β”‚   └── validation.js
    β”œβ”€β”€ middlewares/
    β”‚   β”œβ”€β”€ disabled.js
    β”‚   └── authenticate.js
    β”œβ”€β”€ runtime/
    β”‚   └── runtime.js
    β”œβ”€β”€ node_modules/
    β”œβ”€β”€ package.json
    β”œβ”€β”€ runtime/
    └── .gitignore

Key Modules

β€’ app.js: Entry point of the application. Sets up middleware and routes.
β€’ db.js: Database connection and basic functions using MySQL2 or Postgres.
β€’ logger.js: Configured Winston logger for application-wide logging.
β€’ mailer.js: Nodemailer setup for sending emails.
β€’ userHandler.js: User-related operations, including registration and login.
β€’ authenticate.js: JWT authentication middleware.
β€’ htmlProcessor.js: Functions to process HTML files and strings with placeholders.
β€’ disabled.js: Middleware to disable routes.
β€’ routes/: Directory containing route definitions.

Contributing

We welcome contributions from the community! Please fork the repository and submit a pull request.

  • kush-js: Database engine switcher (interoperability) & Postgres integration
  • u/MinuteScientist7254: Asked the feature to hide userid from token
  • kush-js: Added Bun compatibility

License

This project is licensed under the MIT License.

Contact

If you have any questions, feel free to open an issue or contact us at logan+lightapi@creativehorizons.net

My portfolio

Logan Bunelle

About

A lightweight express API boilerplate, working out of the box !

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published