veko
Version:
๐ Ultra-modern Node.js framework with hot reload, plugins, authentication, and advanced security features
1,899 lines (1,511 loc) โข 51.9 kB
Markdown
# ๐ Veko.js
An **ultra-modern** and **intelligent** web framework for Node.js with Express and EJS, designed for rapid and efficient development with **intelligent hot reload**, **beautiful logging**, **extensible plugin system**, and **revolutionary auto-updater**.
# ๐ Veko.js v1.2.0
An **ultra-modern** and **intelligent** web framework for Node.js with Express and EJS, designed for rapid and efficient development with **intelligent hot reload**, **beautiful logging**, **extensible plugin system**, **advanced code verification**, and the **most advanced auto-updater** in the Node.js ecosystem.
## โจ Features
- ๐ฅ **Intelligent Hot Reload** - Selective reloading of modified routes
- ๐จ **Beautiful Logging** - Colorful logging system with icons and timestamps
- โก **Integrated WebSocket** - Real-time communication for development
- ๐ **Auto-loading** - Routes, views, and middleware auto-configured
- ๐ ๏ธ **Development Mode** - Advanced file monitoring
- ๐ **Smart Prefetching** - Route caching and prefetching
- ๐ **Plugin System** - Extensible architecture with hooks and complete API
- ๐ฃ๏ธ **Dynamic Route Management** - Create/delete routes on-the-fly
- ๐จ **Advanced Layout System** - Powerful templating with sections and helpers
- ๐ฆ **Auto Module Installation** - Automatic dependency management
- ๐ฌ **Advanced Code Verification** - Comprehensive code quality analysis
- ๐ **HTML Reports** - Beautiful verification reports with interactive dashboard
- ๐ **Security Auditing** - Advanced security vulnerability detection
- ๐งฎ **Complexity Analysis** - Cyclomatic complexity and performance metrics
- ๐ **Revolutionary Auto-Updater** - The most advanced auto-updater in Node.js ecosystem
- ๐ก๏ธ **Security-First Updates** - Automatic critical security updates with rollback protection
- ๐พ **Intelligent Backup System** - Smart backups with one-click rollback
- ๐ฏ **Multi-Channel Updates** - Support for stable, beta, and alpha channels
- ๐จ **Interactive CLI** - Beautiful command-line interface with auto-completion
## ๐ Changelog
### ๐ Version 1.2.0 (Latest) - December 2024
#### ๐ Revolutionary Auto-Updater System
- **๐ Advanced Auto-Updater**
- Automatic version checking with intelligent scheduling
- Multi-channel support (stable, beta, alpha, custom registries)
- Security-first approach with cryptographic validation
- Interactive CLI with beautiful colored interface
- Real-time notifications and progress indicators
- Seamless integration with existing applications
- **๐ Security-First Architecture**
- SHA512 integrity verification for all packages
- Automatic security updates with priority handling
- Rollback protection against failed updates
- Vulnerability scanning before installation
- Secure communication protocols
- Audit trail for all operations
- **๐พ Smart Backup & Rollback System**
- Automatic backup before every update
- Configurable backup retention (1-10 backups)
- Instant rollback in case of failure
- Backup size optimization and integrity verification
- Selective file backup with smart exclusions
- Emergency rollback functionality
- **๐จ Interactive CLI Experience**
- Beautiful colored interface with emojis and animations
- Auto-completion for all commands
- Interactive configuration wizard
- Real-time progress bars and status indicators
- Comprehensive help system with examples
- Menu-driven navigation
- **๐ Advanced Monitoring & Statistics**
- Update history tracking with detailed logs
- Performance metrics and success rates
- Rollback statistics and failure analysis
- Update channel analytics
- System health monitoring
- Historical trend analysis
#### ๐ Auto-Updater Quick Start
```bash
# Installation globale recommandรฉe
npm install -g veko
# Commandes disponibles
veko update # Lancer le gestionnaire de mises ร jour interactif
veko update check # Vรฉrifier les mises ร jour
veko update config # Configurer l'auto-updater
veko update fix # Rรฉparer l'auto-updater en cas de problรจme
# Alternative sans installation globale
npx veko update # Utiliser veko update sans installation globale
```
#### ๐ Auto-Updater Simplifiรฉ
L'auto-updater de Veko a รฉtรฉ simplifiรฉ pour plus de stabilitรฉ et d'efficacitรฉ. Il se concentre maintenant sur les commandes essentielles et รฉvite les interfaces interactives qui pourraient interfรฉrer avec le fonctionnement de l'application.
```bash
# Vรฉrifier les mises ร jour
veko update check
# Installer une mise ร jour
veko update update
# Afficher la configuration
veko update config
# Modifier un paramรจtre de configuration
veko update config autoUpdate true
# Afficher les statistiques
veko update stats
# Restaurer une version prรฉcรฉdente
veko update rollback
# Rรฉparer l'auto-updater
veko update fix
# Afficher l'aide et les commandes disponibles
veko update help
```
#### ๐ง Auto-Updater Robuste
L'auto-updater a รฉtรฉ conรงu pour รชtre stable et ne jamais interfรฉrer avec le fonctionnement de l'application principale. Il offre :
- โ
**Installation non-bloquante** - Aucun risque de ralentissement de votre application
- โ
**Mises ร jour automatisรฉes** - Programmez des vรฉrifications rรฉguliรจres
- โ
**Gestion des erreurs avancรฉe** - Restauration automatique en cas d'รฉchec
- โ
**Systรจme de backup** - Avant toute mise ร jour
- โ
**Configuration simple** - Via la ligne de commande
#### ๐ง Auto-Updater Integration
```javascript
const { App } = require('veko');
const app = new App({
// Auto-updater configuration optimisรฉe
autoUpdater: {
enabled: true, // Activer l'auto-updater
checkOnStart: true, // Vรฉrifier au dรฉmarrage
autoUpdate: false, // Mode manuel (recommandรฉ)
updateChannel: 'stable', // stable | beta | alpha
securityUpdates: true, // Auto-installer les mises ร jour de sรฉcuritรฉ
backupCount: 5, // Nombre de backups ร conserver
checkInterval: 3600000, // Vรฉrifier chaque heure (en millisecondes)
rollbackOnFailure: true // Restauration automatique en cas d'รฉchec
}
});
app.listen();
```
#### ๐ RESTful Auto-Updater API
Built-in API endpoints for remote management:
```javascript
// Check for updates programmatically
const updateInfo = await app.checkForUpdates();
console.log(updateInfo); // { hasUpdate: true, currentVersion: '1.0.0', latestVersion: '2.0.0' }
// Perform update
if (updateInfo.hasUpdate) {
const success = await app.performUpdate(updateInfo);
console.log('Update successful:', success);
}
// Rollback if needed
await app.rollbackUpdate();
// Get auto-updater status
const status = app.getAutoUpdaterInfo();
console.log(status); // { active: true, currentVersion: '2.0.0', config: {...}, stats: {...} }
```
#### ๐ก๏ธ RESTful Endpoints
```bash
# Check for updates via HTTP
GET http://localhost:3000/_veko/updates/check
# Trigger update via HTTP
POST http://localhost:3000/_veko/updates/perform
# Get statistics via HTTP
GET http://localhost:3000/_veko/updates/stats
# Perform rollback via HTTP
POST http://localhost:3000/_veko/updates/rollback
```
#### ๐ฏ Update Channels
**Stable Channel (Production)**
```bash
npx veko-update config
# Select "Update Channel" -> "stable"
```
- โ
Production-ready releases
- โ
Thoroughly tested and stable
- โ
Backward compatibility guaranteed
- โ
Recommended for production environments
**Beta Channel (Testing)**
```bash
npx veko-update config
# Select "Update Channel" -> "beta"
```
- โ
Pre-release versions with new features
- โ
Stability testing phase
- โ ๏ธ May contain minor issues
- ๐ฏ Perfect for testing environments
**Alpha Channel (Development)**
```bash
npx veko-update config
# Select "Update Channel" -> "alpha"
```
- โ
Cutting-edge features and improvements
- โ
Immediate access to latest developments
- โ ๏ธ Experimental features included
- ๐ง Development and testing use only
#### ๐ Statistics Dashboard
```bash
npx veko-update stats
```
Comprehensive statistics including:
- ๐ Total updates performed
- โ
Success/failure rates
- ๐ Rollback history and frequency
- ๐ข Update channel distribution
- โก Performance metrics and timing
- ๐ Security update history
- ๐
Last check/update timestamps
### ๐ Previous Versions
#### Version 1.1.0
- Initial plugin system implementation
- Basic layout system with EJS integration
- Hot reload functionality with WebSocket
- CLI commands (dev, setup, build, start)
- Basic logging system with colors
- Auto-loading for routes and middleware
## โจ Features
- ๐ฅ **Intelligent Hot Reload** - Selective reloading of modified routes
- ๐จ **Beautiful Logging** - Colorful logging system with icons and timestamps
- โก **Integrated WebSocket** - Real-time communication for development
- ๐ **Auto-loading** - Routes, views, and middleware auto-configured
- ๐ ๏ธ **Development Mode** - Advanced file monitoring
- ๐ **Smart Prefetching** - Route caching and prefetching
- ๐ **Plugin System** - Extensible architecture with hooks and complete API
- ๐ฃ๏ธ **Dynamic Route Management** - Create/delete routes on-the-fly
- ๐จ **Advanced Layout System** - Powerful templating with sections and helpers
- ๐ฆ **Auto Module Installation** - Automatic dependency management
## ๐ Installation
### Global Installation (CLI)
```bash
npm install -g veko
```
### Project Installation
```bash
npm install veko
```
## ๐ฆ Quick Start
### 1. Create a New Project
```bash
# Create a new project
veko setup my-app
# With options
veko setup --name my-blog --template blog --git
# Available templates: default, api, blog, admin
```
### 2. Basic Application
```javascript
const { App } = require('veko');
const app = new App({
port: 3000,
viewsDir: 'views',
staticDir: 'public',
routesDir: 'routes',
layouts: {
enabled: true,
defaultLayout: 'main'
},
plugins: {
enabled: true,
autoLoad: true,
pluginsDir: 'plugins'
}
});
app.loadRoutes() // Automatically load all routes
.listen();
```
### 3. Ultra-Modern Development Mode
```javascript
const { startDev } = require('veko');
// Simple dev startup
startDev({ port: 3000 });
```
Or directly with the App class:
```javascript
const { App } = require('veko');
const app = new App({
port: 3000,
isDev: true, // Enable development mode
wsPort: 3008, // WebSocket port for hot reload
watchDirs: ['views', 'routes', 'public'], // Watched directories
layouts: {
enabled: true,
defaultLayout: 'main'
},
plugins: {
enabled: true,
autoLoad: true,
pluginsDir: 'plugins'
}
});
app.loadRoutes().listen();
```
### 4. CLI Commands
```bash
# Start development server
veko dev
# Start with custom port
veko dev --port 8080
# Create new project
veko setup my-project
# Create API project
veko setup --template api --name my-api
# Create blog project
veko setup --template blog --name my-blog
# Create admin project
veko setup --template admin --name admin-panel
```
## ๐จ Advanced Layout System
Veko.js includes a powerful layout system that automatically wraps your views with layouts, providing sections, helpers, and dynamic content injection.
### Layout Configuration
```javascript
const app = new App({
layouts: {
enabled: true, // Enable layout system
layoutsDir: 'views/layouts', // Layout directory
defaultLayout: 'main', // Default layout name
extension: '.ejs', // Layout file extension
sections: ['head', 'header', 'content', 'footer', 'scripts'],
cache: true // Cache layouts in production
}
});
```
### Creating Layouts
**views/layouts/main.ejs:**
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= meta.title || 'Veko.js App' %></title>
<% if (meta.description) { %>
<meta name="description" content="<%= meta.description %>">
<% } %>
<!-- Default CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<% if (layout && layout.css) { %>
<% layout.css.forEach(href => { %>
<link rel="stylesheet" href="<%= href %>">
<% }); %>
<% } %>
<!-- Custom head section -->
<% if (sections && sections.head) { %>
<%- sections.head %>
<% } %>
</head>
<body class="<%= layout && layout.bodyClass || '' %>">
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand" href="/">๐ Veko.js</a>
<div class="navbar-nav ms-auto">
<a class="nav-link" href="/">Home</a>
<a class="nav-link" href="/about">About</a>
<a class="nav-link" href="/contact">Contact</a>
</div>
</div>
</nav>
<!-- Custom header -->
<% if (sections && sections.header) { %>
<header>
<%- sections.header %>
</header>
<% } %>
<!-- Main content -->
<main class="container my-4">
<%- sections.content %>
</main>
<!-- Footer -->
<footer class="bg-dark text-light py-4 mt-5">
<div class="container">
<% if (sections && sections.footer) { %>
<%- sections.footer %>
<% } else { %>
<p>© <%= new Date().getFullYear() %> - Powered by Veko.js โก</p>
<% } %>
</div>
</footer>
<!-- JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- Custom JS -->
<% if (layout && layout.js) { %>
<% layout.js.forEach(src => { %>
<script src="<%= src %>"></script>
<% }); %>
<% } %>
<!-- Custom scripts section -->
<% if (sections && sections.scripts) { %>
<%- sections.scripts %>
<% } %>
</body>
</html>
```
### Using Layouts in Views
**views/home.ejs:**
```html
<% layout.title('Home - My App') %>
<% layout.meta('description', 'Welcome to my Veko.js application') %>
<% layout.css('/css/home.css') %>
<% layout.section('header', `
<div class="jumbotron bg-gradient text-center text-white p-5">
<h1>๐ Welcome to Veko.js</h1>
<p>Ultra-modern Node.js framework</p>
</div>
`) %>
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h3>๐ Fast</h3>
<p>Optimized performance and ultra-fast loading times.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h3>๐ Modern</h3>
<p>Cutting-edge technologies and modern architecture.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h3>๐จ Flexible</h3>
<p>Advanced layout system and reusable components.</p>
</div>
</div>
</div>
</div>
<% layout.section('scripts', `
<script>
console.log('โจ Page loaded with Veko.js!');
// Card hover animations
document.querySelectorAll('.card').forEach(card => {
card.addEventListener('mouseenter', function() {
this.style.transform = 'translateY(-5px)';
this.style.transition = 'transform 0.3s ease';
});
card.addEventListener('mouseleave', function() {
this.style.transform = 'translateY(0)';
});
});
</script>
`) %>
```
### Layout Helpers
Available helpers in your views:
```javascript
// Set page title
<% layout.title('My Page Title') %>
// Add meta tags
<% layout.meta('description', 'Page description') %>
<% layout.meta('keywords', 'veko, nodejs, framework') %>
// Add CSS files
<% layout.css('/css/custom.css') %>
<% layout.css('https://cdn.example.com/lib.css') %>
// Add JavaScript files
<% layout.js('/js/custom.js') %>
<% layout.js('https://cdn.example.com/lib.js') %>
// Define sections
<% layout.section('sectionName', 'content') %>
<% layout.section('header', `
<div class="custom-header">
<h1>Custom Header</h1>
</div>
`) %>
```
### Layout Management API
```javascript
// Create a new layout
app.createLayout('admin', customLayoutContent);
// Delete a layout
app.deleteLayout('admin');
// List all available layouts
const layouts = app.listLayouts();
// Reload layouts in development mode
app.reloadLayouts();
// Render with specific layout
app.renderWithCustomLayout(res, 'view', 'admin', data);
// Render without layout
app.renderWithoutLayout(res, 'view', data);
```
## ๐ Plugin System
Veko.js includes a powerful and extensible plugin system that allows adding functionality without modifying the framework core.
### Plugin Configuration
```javascript
const app = new App({
plugins: {
enabled: true, // Enable plugin system
autoLoad: true, // Automatic plugin loading
pluginsDir: 'plugins' // Plugin directory
}
});
```
### Plugin Structure
```javascript
// plugins/my-plugin.js
module.exports = {
name: 'my-plugin',
version: '1.0.0',
description: 'Description of my plugin',
author: 'My Name',
// Dependencies (other required plugins)
dependencies: ['other-plugin'],
// Default configuration
defaultConfig: {
enabled: true,
option1: 'value'
},
// Method called during loading
async load(app, config, context) {
// Your initialization code
context.log('success', 'Plugin loaded!');
// Add a route
context.addRoute('get', '/my-plugin', (req, res) => {
res.json({ message: 'Hello from plugin!' });
});
// Add middleware
context.addMiddleware((req, res, next) => {
req.pluginData = { source: 'my-plugin' };
next();
});
// Add hooks
context.hook('route:create', (method, path) => {
context.log('info', `Route created: ${method} ${path}`);
});
},
// Method called during unloading
async unload(app, config) {
console.log('Plugin unloaded');
},
// Activation/deactivation
async activate(app, config) {
console.log('Plugin activated');
},
async deactivate(app, config) {
console.log('Plugin deactivated');
}
};
```
### Plugin Context API
Each plugin receives a rich context with numerous functionalities:
```javascript
async load(app, config, context) {
// === HOOKS ===
// Add a hook
context.hook('hookName', callback);
context.removeHook('hookName', callback);
// === ROUTES AND MIDDLEWARE ===
// Add a route
context.addRoute('get', '/path', handler);
// Add middleware
context.addMiddleware(middlewareFunction);
// Add CLI command
context.addCommand('name', handler, 'description');
// === LOGGING ===
// Log with automatic plugin name
context.log('success', 'Message', 'details');
// === ACCESS TO OTHER PLUGINS ===
// Get another plugin
const otherPlugin = context.getPlugin('other-plugin');
// List all plugins
const plugins = context.listPlugins();
// === CONFIGURATION ===
// Read config
const config = context.getConfig();
// Modify config
context.updateConfig({ newOption: 'value' });
// === PERSISTENT STORAGE ===
// Save data
context.storage.set('key', 'value');
context.storage.set({ key1: 'value1', key2: 'value2' });
// Read data
const value = context.storage.get('key', 'defaultValue');
const allData = context.storage.get();
// Delete data
context.storage.delete('key');
context.storage.clear();
}
```
### Plugin Management
```javascript
// Load plugin manually
await app.loadPlugin('plugin-name', { option: 'value' });
// Unload plugin
await app.unloadPlugin('plugin-name');
// Reload plugin
await app.reloadPlugin('plugin-name', { newConfig: true });
// List plugins
const plugins = app.listPlugins();
// Enable/disable plugin
await app.plugins.togglePlugin('plugin-name', true);
// Statistics
const stats = app.plugins.getStats();
console.log(`${stats.active}/${stats.total} plugins active`);
```
### Available Hooks
The plugin system provides numerous built-in hooks:
```javascript
// Application hooks
app:init // Application initialization
app:start // Server startup
app:stop // Server shutdown
// Route hooks
route:load // Route loading
route:create // Route creation
route:created // Route created (after)
route:delete // Route deletion
// Request hooks
request:start // Request start
request:end // Request end
// WebSocket hooks
websocket:connect // WebSocket connection
websocket:disconnect // WebSocket disconnection
// File hooks
file:change // File modification
// Plugin hooks
plugin:load // Plugin loading
plugin:unload // Plugin unloading
// Error hooks
error:handle // Error handling
```
### Plugin Examples
#### Database Plugin
```javascript
// plugins/database.js
const mongoose = require('mongoose');
module.exports = {
name: 'database',
version: '1.0.0',
description: 'MongoDB connection plugin',
defaultConfig: {
uri: 'mongodb://localhost:27017/myapp',
options: {
useNewUrlParser: true,
useUnifiedTopology: true
}
},
async load(app, config, context) {
try {
await mongoose.connect(config.uri, config.options);
context.log('success', 'MongoDB connection established');
// Expose mongoose in app
app.db = mongoose;
// Shutdown hook
context.hook('app:stop', async () => {
await mongoose.disconnect();
context.log('info', 'MongoDB connection closed');
});
} catch (error) {
context.log('error', 'MongoDB connection error', error.message);
throw error;
}
},
async unload() {
await mongoose.disconnect();
}
};
```
#### Authentication Plugin
```javascript
// plugins/auth.js
const jwt = require('jsonwebtoken');
module.exports = {
name: 'auth',
version: '1.0.0',
description: 'JWT authentication plugin',
dependencies: ['database'],
defaultConfig: {
secret: 'your-secret-key',
expiresIn: '24h'
},
async load(app, config, context) {
// Authentication middleware
const authMiddleware = (req, res, next) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Missing token' });
}
try {
const decoded = jwt.verify(token, config.secret);
req.user = decoded;
next();
} catch (error) {
res.status(401).json({ error: 'Invalid token' });
}
};
// Authentication routes
context.addRoute('post', '/auth/login', async (req, res) => {
const { email, password } = req.body;
// User validation (example)
const user = await validateUser(email, password);
if (user) {
const token = jwt.sign(
{ id: user.id, email: user.email },
config.secret,
{ expiresIn: config.expiresIn }
);
res.json({ token, user });
} else {
res.status(401).json({ error: 'Invalid credentials' });
}
});
context.addRoute('get', '/auth/profile', authMiddleware, (req, res) => {
res.json({ user: req.user });
});
// Expose middleware in app
app.authMiddleware = authMiddleware;
context.log('success', 'Authentication plugin loaded');
}
};
```
#### Cache Plugin
```javascript
// plugins/cache.js
const NodeCache = require('node-cache');
module.exports = {
name: 'cache',
version: '1.0.0',
description: 'In-memory cache plugin',
defaultConfig: {
stdTTL: 600, // 10 minutes
checkperiod: 120
},
async load(app, config, context) {
const cache = new NodeCache(config);
// Cache middleware
const cacheMiddleware = (duration = 300) => {
return (req, res, next) => {
const key = req.originalUrl;
const cached = cache.get(key);
if (cached) {
context.log('info', 'Cache hit', key);
return res.json(cached);
}
const originalSend = res.json;
res.json = function(data) {
cache.set(key, data, duration);
context.log('info', 'Cache set', key);
return originalSend.call(this, data);
};
next();
};
};
// Cache API
app.cache = {
get: (key) => cache.get(key),
set: (key, value, ttl) => cache.set(key, value, ttl),
del: (key) => cache.del(key),
flush: () => cache.flushAll(),
middleware: cacheMiddleware,
stats: () => cache.getStats()
};
// Stats route
context.addRoute('get', '/cache/stats', (req, res) => {
res.json(cache.getStats());
});
context.log('success', 'Cache plugin loaded');
}
};
```
### Inline Plugin Creation
```javascript
// Create a simple plugin directly in code
const simplePlugin = app.plugins.createSimplePlugin(
'logger-plugin',
(app, config, context) => {
// Hook to log all requests
context.hook('request:start', (req) => {
context.log('info', `${req.method} ${req.url}`);
});
// Debug route
context.addRoute('get', '/debug/logs', (req, res) => {
res.json({
plugin: 'logger-plugin',
requests: context.storage.get('requestCount', 0)
});
});
},
{
version: '1.0.0',
description: 'Request logging plugin'
}
);
await app.loadPlugin(simplePlugin);
```
## ๐ฃ๏ธ Dynamic Route Management
Veko.js allows creating, modifying, and deleting routes dynamically during runtime.
### Dynamic Route Creation
```javascript
// Create route on-the-fly
app.createRoute('get', '/api/dynamic', (req, res) => {
res.json({ message: 'Route created dynamically!' });
});
// With middleware
app.createRoute('post', '/api/secure', [
authMiddleware,
validationMiddleware,
(req, res) => {
res.json({ success: true });
}
]);
// Update existing route
app.updateRoute('get', '/api/dynamic', (req, res) => {
res.json({ message: 'Route updated!' });
});
// Delete route
app.deleteRoute('get', '/api/dynamic');
```
### Creating Route Files
```javascript
// Create physical route file
app.createRouteFile('/users/profile', {
get: (req, res) => {
res.render('profile', { user: req.user });
},
post: (req, res) => {
// Profile update
res.json({ updated: true });
}
}, {
description: 'User profile management',
middleware: [authMiddleware]
});
// Delete route file
app.deleteRouteFile('/users/profile');
// List all routes
const routes = app.listRoutes();
console.log(routes);
```
## ๐จ Beautiful Logging
Veko.js offers a revolutionary logging system with:
- ๐ **Formatted timestamps**
- ๐ฏ **Colored badges** with Unicode icons
- ๐ **Specialized log types**
- ๐ **Optimized ANSI colors**
### Available Log Types
```javascript
app.log('success', 'Operation successful', 'โ
Additional details');
app.log('error', 'Critical error', 'โ Error message');
app.log('warning', 'Warning', 'โ ๏ธ Warning message');
app.log('info', 'Information', 'โน๏ธ General info');
app.log('server', 'Server', '๐ Server startup');
app.log('route', 'Route', '๐ New route');
app.log('dev', 'Development', '๐ ๏ธ Dev mode');
app.log('file', 'File', '๐ File modification');
app.log('reload', 'Reload', '๐ Hot reload');
app.log('create', 'Creation', 'โ Element created');
app.log('delete', 'Deletion', '๐๏ธ Element deleted');
app.log('install', 'Installation', '๐ฆ Module installed');
```
### Console Output Example
```
[2:32:15 PM] โจ Server started successfully ๐ http://localhost:3000
[2:32:16 PM] ๐ ๏ธ Development mode active ๐ฅ Intelligent hot reload on port 3008
[2:32:16 PM] ๐ Plugin database loaded database v1.0.0
[2:32:16 PM] ๐ Plugin auth loaded auth v1.0.0
[2:32:17 PM] ๐ Plugin system ๐ 2/2 plugins active
[2:32:17 PM] ๐ Route loaded index.js โ /
[2:32:18 PM] โ Route created dynamically GET /api/users
[2:32:19 PM] ๐ File modified ๐ routes/users.js
[2:32:19 PM] ๐ Route reloaded ๐ routes/users.js
```
## ๐ฅ Intelligent Hot Reload
### Selective Reload by File Type
- **Modified routes** โ Route-only reload
- **Modified views** โ Light template reload
- **Static files** โ Full browser reload
- **Modified plugins** โ Specific plugin reload
- **Modified layouts** โ Layout cache clear and reload
### Hot Reload Configuration
```javascript
const app = new App({
isDev: true,
wsPort: 3008, // WebSocket port
watchDirs: ['views', 'routes', 'public', 'src', 'plugins'], // Watched directories
prefetch: {
enabled: true,
maxConcurrent: 3,
notifyUser: true,
cacheRoutes: true,
prefetchDelay: 1000
}
});
```
### Automatic Client Script
The hot reload script is automatically injected into your pages:
```javascript
// Automatically injected before </body>
<script>
(function() {
const ws = new WebSocket('ws://localhost:3008');
ws.onopen = () => console.log('๐ Veko.js connected');
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
switch(data.type) {
case 'reload':
console.log('๐ Full reload...');
setTimeout(() => window.location.reload(), 300);
break;
case 'route-reload':
console.log('๐ Route reloaded:', data.route);
if (window.location.pathname === data.route) {
setTimeout(() => window.location.reload(), 300);
}
break;
case 'route-created':
console.log('โ Route created:', data.method, data.path);
break;
case 'route-deleted':
console.log('๐๏ธ Route deleted:', data.method, data.path);
break;
case 'view-reload':
console.log('๐จ View reloaded:', data.file);
setTimeout(() => window.location.reload(), 300);
break;
case 'layout-reload':
console.log('๐จ Layout reloaded:', data.file);
setTimeout(() => window.location.reload(), 300);
break;
case 'plugin-reload':
console.log('๐ Plugin reloaded:', data.plugin);
break;
}
};
})();
</script>
```
## ๐ Project Structure
```
my-project/
โโโ routes/
โ โโโ index.js # Route: /
โ โโโ about.js # Route: /about
โ โโโ users/
โ โ โโโ index.js # Route: /users
โ โ โโโ [id].js # Route: /users/:id
โ โโโ api/
โ โโโ products.js # Route: /api/products
โโโ views/
โ โโโ layouts/ # Layout templates
โ โ โโโ main.ejs # Main layout
โ โ โโโ admin.ejs # Admin layout
โ โโโ partials/ # Reusable components
โ โโโ index.ejs
โ โโโ about.ejs
โโโ public/
โ โโโ css/
โ โโโ js/
โ โโโ images/
โโโ plugins/ # Custom plugins
โ โโโ database.js
โ โโโ auth.js
โ โโโ cache.js
โโโ data/ # Plugin data
โ โโโ plugins/
โ โโโ database.json
โ โโโ auth.json
โโโ config/ # Configuration files
โ โโโ app.js
โโโ middleware/ # Custom middleware
โโโ package.json
```
## ๐ฃ๏ธ Route System
### Automatic Routes
Veko.js automatically loads all routes from the `routes/` folder. The filename determines the URL:
- `routes/index.js` โ `/`
- `routes/about.js` โ `/about`
- `routes/users/profile.js` โ `/users/profile`
- `routes/api/users.js` โ `/api/users`
### Dynamic Parameters
Use brackets for parameters:
- `routes/users/[id].js` โ `/users/:id`
- `routes/posts/[slug]/comments.js` โ `/posts/:slug/comments`
### Route File Formats
#### HTTP Methods (Recommended)
```javascript
// routes/users.js
module.exports = {
// GET /users
get: (req, res) => {
res.render('users', { users: [] });
},
// POST /users
post: (req, res) => {
const newUser = req.body;
res.status(201).json({ user: newUser });
},
// PUT /users
put: (req, res) => {
res.json({ message: 'User updated' });
},
// DELETE /users
delete: (req, res) => {
res.json({ message: 'User deleted' });
}
};
```
#### Custom Function
```javascript
// routes/custom.js
module.exports = (app) => {
app.get('/custom', (req, res) => {
res.json({ message: 'Custom route' });
});
app.post('/custom/:action', middleware, (req, res) => {
res.json({ action: req.params.action });
});
};
```
## โก Ultra-Modern Development Mode
Development mode includes:
- ๐ฅ **Intelligent Hot Reload** - Selective reload by file type
- ๐ก **WebSocket** - Real-time server โ client communication
- ๐จ **Colorful Logs** - Logging system with badges and icons
- ๐ **Advanced Monitoring** - File monitoring with chokidar
- โก **Performance** - Reload only modified parts
- ๐ ๏ธ **Debugging** - Detailed error messages with stack traces
- ๐ **Hot Plugin Reload** - Real-time plugin reloading
### Complete Configuration
```javascript
const app = new App({
port: 3000,
wsPort: 3008,
viewsDir: 'views',
staticDir: 'public',
routesDir: 'routes',
isDev: true,
watchDirs: ['views', 'routes', 'public', 'src', 'plugins'],
errorLog: 'error.log',
showStack: true,
autoInstall: true,
layouts: {
enabled: true,
defaultLayout: 'main',
layoutsDir: 'views/layouts',
cache: false // Disable cache in dev mode
},
plugins: {
enabled: true,
autoLoad: true,
pluginsDir: 'plugins'
},
prefetch: {
enabled: true,
maxConcurrent: 3,
notifyUser: true,
cacheRoutes: true,
prefetchDelay: 1000
}
});
```
### Advanced Error Handling
```javascript
// Automatic handling of uncaught errors
process.on('uncaughtException', (error) => {
app.log('error', 'Uncaught exception', error.message);
// Automatic WebSocket notification to clients
});
process.on('unhandledRejection', (reason) => {
app.log('error', 'Promise rejected', reason.toString());
// Automatic error broadcast
});
```
## ๐จ Views with EJS
Veko.js uses EJS as the default template engine with advanced configuration.
### View Configuration
```javascript
// Automatic view directory configuration
this.express.set('view engine', 'ejs');
this.express.set('views', [
path.join(process.cwd(), this.options.viewsDir), // Project directory
path.join(process.cwd(), this.options.layouts.layoutsDir), // Layouts directory
path.join(__dirname, '..', 'views'), // Veko.js views
path.join(__dirname, '..', 'error') // Error pages
]);
```
### Modern View Example
```html
<!-- views/index.ejs -->
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel="stylesheet" href="/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<header>
<h1><%= message %></h1>
</header>
<main>
<% if (users && users.length > 0) { %>
<section class="users">
<% users.forEach(user => { %>
<article class="user-card">
<h3><%= user.name %></h3>
<p><%= user.email %></p>
</article>
<% }); %>
</section>
<% } else { %>
<p>No users found.</p>
<% } %>
</main>
<!-- Hot reload script automatically injected in dev mode -->
</body>
</html>
```
## ๐ฆ Auto Module Installation
Veko.js automatically installs missing dependencies during initialization.
### Module Installation Configuration
```javascript
const app = new App({
autoInstall: true // Enable automatic installation (default: true)
});
// Check if module is available
if (app.isModuleAvailable('express')) {
console.log('Express is available');
}
// Install module on-the-fly
await app.installModule('lodash', '4.17.21');
// Require module with auto-installation
const lodash = await app.requireModule('lodash');
```
### Required Modules
Veko.js automatically manages these dependencies:
```javascript
const requiredModules = {
'express': '^4.18.2',
'ejs': '^3.1.9',
'ws': '^8.14.2',
'chokidar': '^3.5.3',
'chalk': '^4.1.2',
'commander': '^11.1.0'
};
```
## ๐ง Complete API
### App Class
#### Constructor
```javascript
const app = new App({
port: 3000, // Listening port
wsPort: 3008, // WebSocket port (dev mode)
viewsDir: 'views', // Views directory
staticDir: 'public', // Static directory
routesDir: 'routes', // Routes directory
isDev: false, // Development mode
watchDirs: ['views', 'routes', 'public'], // Watched directories
errorLog: 'error.log', // Error log file
showStack: true, // Show stack trace
autoInstall: true, // Auto module installation
layouts: { // Layout configuration
enabled: true,
defaultLayout: 'main',
layoutsDir: 'views/layouts',
extension: '.ejs',
cache: true
},
plugins: { // Plugin configuration
enabled: true,
autoLoad: true,
pluginsDir: 'plugins'
},
prefetch: { // Prefetch configuration
enabled: true,
maxConcurrent: 3,
notifyUser: true,
cacheRoutes: true,
prefetchDelay: 1000
}
});
```
#### Main Methods
```javascript
// Loading and startup
app.loadRoutes(routesDir) // Load routes automatically
app.listen(port) // Start server
app.startDev(port) // Start in development mode
app.stop() // Stop server
// Middleware and configuration
app.use(middleware) // Add middleware
app.setupExpress() // Configure Express
app.setupDevMode() // Enable development mode
// Beautiful logging
app.log(type, message, details) // Advanced logging system
// Dynamic route management
app.createRoute(method, path, handler, options) // Create route dynamically
app.deleteRoute(method, path) // Delete route
app.updateRoute(method, path, newHandler) // Update route
app.createRouteFile(routePath, handlers, options) // Create route file
app.deleteRouteFile(routePath) // Delete route file
app.listRoutes() // List all routes
// Layout management
app.createLayout(name, content) // Create layout
app.deleteLayout(name) // Delete layout
app.listLayouts() // List layouts
app.reloadLayouts() // Reload layouts
app.renderWithCustomLayout(res, view, layout, data) // Render with specific layout
app.renderWithoutLayout(res, view, data) // Render without layout
// Module management
app.installModule(name, version) // Install module
app.isModuleAvailable(name) // Check module availability
app.requireModule(name, version) // Require with auto-install
// Route management (internal)
app.reloadSpecificRoute(filePath) // Reload specific route
app.removeRoute(filePath) // Remove route
app.filePathToRoute(filePath) // Convert path โ route
// WebSocket and broadcast
app.broadcast(data) // Broadcast WebSocket message
app.sendAvailableRoutes(ws) // Send available routes
// Plugin management
app.loadPlugin(plugin, config) // Load plugin
app.unloadPlugin(pluginName) // Unload plugin
app.reloadPlugin(pluginName, config) // Reload plugin
app.listPlugins() // List plugins
app.executeHook(hookName, ...args) // Execute hook
```
### Utility Functions
```javascript
const { createApp, startDev, start } = require('veko');
// Quick app creation
const app = createApp({ port: 3000 });
// Development startup
startDev({ port: 3000, watchDirs: ['src'] });
// Production startup
start({ port: 8080 });
```
## ๐ Advanced Examples
### Complete Application with Plugins
```javascript
const { App } = require('veko');
const app = new App({
port: 3000,
isDev: process.env.NODE_ENV === 'development',
layouts: {
enabled: true,
defaultLayout: 'main'
},
plugins: {
enabled: true,
autoLoad: true,
pluginsDir: 'plugins'
}
});
// Load plugins with specific configuration
await app.loadPlugin('database', {
uri: process.env.MONGODB_URI || 'mongodb://localhost:27017/myapp'
});
await app.loadPlugin('auth', {
secret: process.env.JWT_SECRET || 'development-secret',
expiresIn: '7d'
});
await app.loadPlugin('cache', {
stdTTL: 3600 // 1 hour
});
// Add global hooks
app.plugins.addHook('request:start', (req) => {
app.log('info', `${req.method} ${req.url}`, `IP: ${req.ip}`);
});
app.plugins.addHook('error:handle', (error, req) => {
app.log('error', 'Application error', `${error.message} - ${req.url}`);
});
app.loadRoutes().listen();
```
### Modern REST API with Error Handling
```javascript
// routes/api/users.js
const users = [];
module.exports = {
get: async (req, res) => {
try {
// Use plugin cache
const cacheKey = `users:${JSON.stringify(req.query)}`;
const cached = req.app.cache?.get(cacheKey);
if (cached) {
return res.json(cached);
}
// Database simulation
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const start = (page - 1) * limit;
const end = start + limit;
const paginatedUsers = users.slice(start, end);
const result = {
users: paginatedUsers,
pagination: {
page,
limit,
total: users.length,
pages: Math.ceil(users.length / limit)
}
};
// Cache result
req.app.cache?.set(cacheKey, result, 300);
res.json(result);
} catch (error) {
res.status(500).json({ error: 'Server error' });
}
},
post: [
// Use auth middleware from plugin
(req, res, next) => req.app.authMiddleware?.(req, res, next) || next(),
async (req, res) => {
try {
const { name, email } = req.body;
if (!name || !email) {
return res.status(400).json({
error: 'Name and email required'
});
}
const newUser = {
id: users.length + 1,
name,
email,
createdAt: new Date().toISOString()
};
users.push(newUser);
// Invalidate cache
req.app.cache?.del('users:*');
res.status(201).json({
message: 'User created',
user: newUser
});
} catch (error) {
res.status(500).json({ error: 'Server error' });
}
}
]
};
```
### Advanced Plugin with CLI
```javascript
// plugins/admin.js
module.exports = {
name: 'admin',
version: '2.0.0',
description: 'Advanced administration plugin',
dependencies: ['database', 'auth'],
defaultConfig: {
adminPath: '/admin',
secretKey: 'admin-secret'
},
async load(app, config, context) {
// Admin-only middleware
const adminMiddleware = (req, res, next) => {
if (!req.user || req.user.role !== 'admin') {
return res.status(403).json({ error: 'Access denied' });
}
next();
};
// Administration routes
context.addRoute('get', `${config.adminPath}/dashboard`, [
app.authMiddleware,
adminMiddleware,
(req, res) => {
res.render('admin/dashboard', {
layout: 'admin',
stats: context.storage.get('stats', {}),
plugins: context.listPlugins()
});
}
]);
// Plugin management API
context.addRoute('post', `${config.adminPath}/plugins/:name/toggle`, [
app.authMiddleware,
adminMiddleware,
async (req, res) => {
try {
const { name } = req.params;
const result = await context.getPlugin('plugins')?.togglePlugin(name);
res.json({ success: true, active: result });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
]);
// CLI commands
context.addCommand('admin:stats', () => {
console.log('=== Admin Statistics ===');
console.log('Plugins:', context.listPlugins().length);
console.log('Stats:', context.storage.get('stats', {}));
}, 'Display administration statistics');
context.addCommand('admin:reset', () => {
context.storage.clear();
console.log('Admin data reset');
}, 'Reset administration data');
// Hook to collect stats
context.hook('request:end', (req, res) => {
const stats = context.storage.get('stats', { requests: 0, errors: 0 });
stats.requests++;
if (res.statusCode >= 400) {
stats.errors++;
}
context.storage.set('stats', stats);
});
context.log('success', 'Admin plugin loaded', `Interface: ${config.adminPath}`);
}
};
```
## ๐ Deployment
### Production Mode
```javascript
// app.js
const { App } = require('veko');
const app = new App({
port: process.env.PORT || 3000,
isDev: false, // Disable development mode
errorLog: 'logs/error.log',
showStack: false, // Hide stack traces in production
autoInstall: false, // Disable auto-installation in production
layouts: {
enabled: true,
cache: true // Enable layout caching
},
plugins: {
enabled: true,
autoLoad: true,
pluginsDir: 'plugins'
}
});
app.loadRoutes().listen();
```
### Environment Variables
```bash
# .env
NODE_ENV=production
PORT=8080
LOG_LEVEL=info
MONGODB_URI=mongodb://prod-server:27017/myapp
JWT_SECRET=super-secret-production-key
```
### Docker
```dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
```
## ๐ Performance and Optimization
### Smart Prefetching
```javascript
const app = new App({
prefetch: {
enabled: true, // Enable prefetching
maxConcurrent: 3, // Max simultaneous requests
notifyUser: true, // Notify user
cacheRoutes: true, // Route caching
prefetchDelay: 1000 // Delay before prefetching
}
});
```
### Performance Monitoring
```javascript
// Monitoring plugin
const monitoringPlugin = {
name: 'monitoring',
async load(app, config, context) {
context.hook('request:start', (req) => {
req.startTime = process.hrtime.bigint();
});
context.hook('request:end', (req, res) => {
const duration = Number(process.hrtime.bigint() - req.startTime) / 1000000;
if (duration > 100) {
context.log('warning', 'Slow request detected',
`${req.method} ${req.url} - ${duration.toFixed(2)}ms`);
}
// Store metrics
const metrics = context.storage.get('metrics', { slow: 0, total: 0 });
metrics.total++;
if (duration > 100) metrics.slow++;
context.storage.set('metrics', metrics);
});
}
};
app.loadPlugin(monitoringPlugin);
```
## ๐ ๏ธ CLI Commands
### Setup Commands
```bash
# Create new project with default template
veko setup my-app
# Create with specific template
veko setup --template api --name my-api
veko setup --template blog --name my-blog
veko setup --template admin --name admin-panel
# Setup with options
veko setup my-app --git --skip-install
veko setup --name my-app --dir ./projects/my-app
```
### Development Commands
```bash
# Start development server
veko dev
# Start with custom port
veko dev --port 8080
# Start with custom watch directories
veko dev --watch "src,views,routes"
# Start with custom entry file
veko dev --file ./src/server.js
```
### Build Commands
```bash
# Build for production
veko build
# Start production server
veko start
```
## ๐ค Contributing
Contributions are welcome!
### Local Development
```bash
git clone https://github.com/username/veko.js.git
cd veko.js
npm install
npm run dev
```
### Testing
```bash
npm test
npm run test:watch
npm run test:coverage
```
### Creating a Plugin
1. Create a file in `plugins/`
2. F