nilajs v1.2.2
NilaJS REST api framework
npx nilajs create:app <appName>Steps to run the application REST api
cd <appName>
npm install
npm startTo use nila cli to generate files
Run below command to generate a new module with basic CRUD operations
Run below command to generate a new controller
node nila create:controller <controllerName>Run below command to generate a new model
node nila create:model <modelName>š NilaJS - Scalable & Lightweight JS backend framework
NilaJS is a Scalable & Lightweight JS backend framework designed with a clean architecture, following MVC (Model-View-Controller) and service-based design patterns. It includes authentication, validations, error handling, socket handling, and a structured folder hierarchy for easy scalability.
š Folder Structure
NilaJS
āāā config/
ā āāā mongoose.js
āāā controllers/
ā āāā user.controller.js
āāā models/
ā āāā user.model.js
āāā node_modules/
āāā routes/v1/
ā āāā routes.js
āāā services/user/
ā āāā auth.service.js
ā āāā user.service.js
āāā utils/
ā āāā error.handler.js
ā āāā memory.handler.js
ā āāā socket.handler.js
āāā validations/user/
ā āāā auth.validation.js
ā āāā user.validation.js
āāā .env
āāā .env.example
āāā .gitignore
āāā index.js
āāā package-lock.json
āāā package.json
āāā README.mdš Folder & File Explanations
1ļøā£ config/
Stores configuration files for external services or database connections.
mongoose.jsā Initializes and configures MongoDB using Mongoose.
2ļøā£ controllers/
Contains route handlers that process requests and return responses.
user.controller.jsā Manages user-related logic (e.g., register, login, CRUD).
3ļøā£ models/
Defines database schemas and models using Mongoose.
user.model.jsā Defines the User schema with fields likename,email,password.
4ļøā£ routes/v1/
Defines API routes and maps them to controllers.
routes.jsā Centralized API routing for version 1 (v1).
5ļøā£ services/user/
Contains business logic and reusable service functions.
auth.service.jsā Handles authentication logic (e.g., password hashing, token generation).user.service.jsā Handles user-related operations (e.g., CRUD, profile updates).
6ļøā£ utils/
Utility functions for handling errors, memory monitoring, and sockets.
error.handler.jsā Centralized error handling middleware.memory.handler.jsā Monitors memory usage and prevents leaks.socket.handler.jsā Handles WebSocket (socket.io) connections.
7ļøā£ validations/user/
Handles request validations using libraries like Joi or Express Validator.
auth.validation.jsā Validates authentication-related requests (e.g., login, register).user.validation.jsā Validates user data inputs for CRUD operations.
8ļøā£ Root Files
.envā Stores environment variables (e.g., database URL, API keys)..env.exampleā Example.envfile for developers..gitignoreā Prevents unnecessary files from being committed.index.jsā Entry point of the application, initializes Express server.package.jsonā Lists dependencies, scripts, and project metadata.README.mdā Documentation for the project.
Environment Configuration Guide
This document provides instructions on how to set up and configure the environment variables for the application.
Prerequisites
- Ensure you have Node.js installed on your system.
- A
.envfile should be created in the root directory of the project. - Do not expose sensitive credentials in public repositories.
Setup Instructions
Create a
.envfile in the root directory of the project if it does not already exist.Copy the following template into your
.envfile and update the values accordingly:
# Node.js environment setting
NODE_ENV=development
# Application server port
PORT=3000
# Application name
APP_NAME="YourAppName"
# Base URL of the application
APP_URL="http://localhost:3000"
# MongoDB connection string
MONGODB_URL="mongodb://localhost:27017/mydatabase"
# Secret key for encryption/decryption
SECRET_KEY="SECRET_KEY_FOR_ENCRYPTION_AND_DECRYPTION"
# JWT secret key for authentication
JWT_SECRET="JWT_SECRET_KEY"
# Whitelisted domains for CORS (comma-separated values)
WHITE_LISTED_DOMAINS="http://localhost:4200, http://localhost:3000"
# SMTP email configurations
SMTP_HOST=smtp.zoho.com
SMTP_PORT=465
SMTP_USERNAME=email@mailinator.com
SMTP_PASSWORD=appPassword
EMAIL_FROM=email@mailinator.com Notes:
- Update all values as per your environment.
- Use strong and unique keys for
SECRET_KEYandJWT_SECRET. - For production, avoid hardcoding secrets; use a secure vault or environment variable manager.
- Ensure the
.envfile is added to.gitignoreto prevent exposing sensitive information.
Security Considerations
- Never commit the
.envfile to version control. - Use environment variable management tools like dotenv, AWS Secrets Manager, or Vault for production setups.
- Regularly rotate your secret keys and credentials.
MIT License
Copyright (c) 2023 Sivabharathy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.