Skip to content

ersinkoc/Spark

Repository files navigation

⚑ Spark

A fast, lightweight, and zero-dependency Node.js web framework built for performance and simplicity.

npm version License: MIT Node.js Version

πŸš€ Features

  • Zero Dependencies - No external dependencies, pure Node.js
  • High Performance - Built for speed with optimized request handling
  • Middleware Support - Express-like middleware system
  • Advanced Router - Powerful routing with parameter support
  • Session Management - Built-in session handling with auto-save
  • Security First - CORS, CSRF, rate limiting, and security headers
  • TypeScript Support - Full TypeScript definitions included
  • Memory Efficient - Optimized for low memory usage
  • Easy to Use - Simple, intuitive API

πŸ“¦ Installation

npm install @oxog/spark

🌟 Quick Start

const { Spark } = require('@oxog/spark');

const app = new Spark();

app.get('/', (ctx) => {
  ctx.json({ message: 'Hello World!' });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

πŸ“š Documentation

🎯 Quick Links

πŸ“ˆ Learning Path

πŸ›‘οΈ Production Ready

πŸ”§ Core Components

Application

The main Spark application class that handles server lifecycle, middleware, and routing.

Router

Advanced routing system with support for parameters, middleware, and nested routes.

Context

Request/response context object with helper methods for common operations.

Middleware

Extensible middleware system with built-in middleware for common tasks.

πŸ› οΈ Built-in Middleware

  • Body Parser - Parse JSON, form data, and text
  • CORS - Cross-origin resource sharing
  • Session - Session management with auto-save functionality
  • Security - Security headers and protection
  • Rate Limiting - Request rate limiting
  • Compression - Response compression
  • Static Files - Static file serving
  • Logger - Request logging
  • Health Check - Health monitoring endpoints

πŸ“Š Performance

Spark is designed for high performance with minimal overhead:

  • ~4000 req/sec on standard hardware
  • <1ms average response time
  • <50MB memory footprint
  • Zero external dependencies

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Application   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Middleware    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     Router      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    Context      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   HTTP Server   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”’ Security

Spark includes built-in security features:

  • CORS protection
  • CSRF protection
  • Rate limiting
  • Security headers
  • Input validation
  • XSS protection

πŸ§ͺ Testing

# Run all tests
npm test

# Run specific test suites
npm run test:unit
npm run test:integration
npm run test:examples

# Run performance benchmarks
npm run benchmark

πŸ“ˆ Examples

Basic API

const { Spark } = require('@oxog/spark');

const app = new Spark();

app.get('/users', (ctx) => {
  ctx.json([
    { id: 1, name: 'John Doe' },
    { id: 2, name: 'Jane Smith' }
  ]);
});

app.get('/users/:id', (ctx) => {
  const { id } = ctx.params;
  ctx.json({ id, name: 'User ' + id });
});

app.listen(3000);

With Middleware

const { Spark } = require('@oxog/spark');

const app = new Spark();

// Global middleware
app.use(require('@oxog/spark/middleware/logger')());
app.use(require('@oxog/spark/middleware/cors')());

// Route-specific middleware
app.get('/protected', 
  require('@oxog/spark/middleware/auth'),
  (ctx) => {
    ctx.json({ message: 'Protected route' });
  }
);

app.listen(3000);

E-commerce API with Sessions

const { Spark, Router } = require('@oxog/spark');

const app = new Spark();
const api = new Router();

// Session middleware with auto-save
app.use(require('@oxog/spark/middleware/session')({
  secret: 'your-secret-key',
  saveUninitialized: true,
  cookie: {
    maxAge: 24 * 60 * 60 * 1000, // 24 hours
    httpOnly: true,
    secure: process.env.NODE_ENV === 'production'
  }
}));

// Authentication
api.post('/auth/login', (ctx) => {
  const { email, password } = ctx.body;
  
  if (authenticate(email, password)) {
    ctx.session.userId = user.id; // Auto-saved immediately
    ctx.json({ success: true });
  } else {
    ctx.status(401).json({ error: 'Invalid credentials' });
  }
});

// Protected routes
api.get('/orders', requireAuth, (ctx) => {
  const orders = getOrdersByUser(ctx.session.userId);
  ctx.json({ orders });
});

app.use('/api', api.routes());
app.listen(3000);

🀝 Contributing

Contributions are welcome! Please read our Contributing Guide for details.

πŸ“„ License

MIT License - see the LICENSE file for details.

πŸ”— Links

🌟 Support

If you find Spark useful, please consider giving it a star on GitHub!


Built with ❀️ by the Spark team

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •