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.
Date format: YYYY-MM-DD
- ๐ฐ Added Bun compatibility (kush-js)
- ๐ง Added a middleware to disable routes easily:
app.get('/register', disabled, (req, res) => {
=> This route now returns a 403 error
- ๐ 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 :))
- ๐ Initial release
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.
Make sure you have Node.js installed on your machine.
-
Clone the repository:
git clone https://github.com/yourusername/lightapi.git cd lightapi
-
Install dependencies:
npm install
Or with Bun
bun install
-
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>'
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();
- Start the server:
with Bun:
node app.js
bun run app.js
Your API will be running on http://localhost:5005.
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
โข 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.
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
This project is licensed under the MIT License.
If you have any questions, feel free to open an issue or contact us at logan+lightapi@creativehorizons.net