/light-api

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

Primary LanguageJavaScriptMIT LicenseMIT

๐ŸŒŸ 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