Documentation
Learn how to build modern web applications with Aroma.js Framework.
Installation
npm install aroma.js
Basic Usage
import Aroma from 'aroma.js';
const app = new Aroma();
// Middleware example
app.use((req, res, next) => {
console.log(`Request received: ${req.method} ${req.url}`);
next();
});
// Simple route
app.get('/', (req, res) => {
res.send({ message: 'Hello, world!' });
});
// Start server
app.listen(3000, () => {
console.log('Aroma.js server running on http://localhost:3000');
});
Features
// Define multiple routes
app.get('/about', (req, res) => {
res.send({ message: 'About page' });
});
app.post('/submit', (req, res) => {
res.send({ message: 'Data received' });
});
// Middleware example for authentication
app.use((req, res, next) => {
if (!req.headers.authorization) {
return res.status(403).send({ error: 'Unauthorized' });
}
next();
});
Template Rendering
import Aroma from 'aroma.js';
import path from 'path';
const app = new Aroma();
// Set up template rendering
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index', { title: 'Welcome to Aroma.js' });
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Error Handling
// Global error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send({ error: 'Something went wrong!' });
});
Database Integration
import Aroma from 'aroma.js';
import { MongoClient } from 'mongodb';
const app = new Aroma();
const client = new MongoClient('mongodb://localhost:27017');
app.get('/users', async (req, res) => {
try {
await client.connect();
const db = client.db('mydatabase');
const users = await db.collection('users').find().toArray();
res.send(users);
} catch (error) {
res.status(500).send({ error: 'Database connection error' });
}
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Router Example
import Aroma from 'aroma.js';
const app = new Aroma();
const router = app.Router();
// Define routes
router.get('/home', (req, res) => {
res.send({ message: 'Welcome to Home Page' });
});
router.post('/login', (req, res) => {
res.send({ message: 'Login Successful' });
});
router.put('/update', (req, res) => {
res.send({ message: 'Update Successful' });
});
router.delete('/delete', (req, res) => {
res.send({ message: 'Delete Successful' });
});
// Use router in app
app.use('/api', router);
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Cookie Handling
import Aroma from 'aroma.js';
const app = new Aroma();
app.useCookies();
app.get('/set-cookie', (req, res) => {
// Set a cookie
res.manageCookies('username', 'AromaUser', { httpOnly: true, maxAge: 3600 });
res.send('Cookie has been set');
});
app.get('/get-cookie', (req, res) => {
// Retrieve a cookie
const username = req.cookies.username;
res.send(`Username from cookie: ${username}`);
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Serving Static Files
import Aroma from 'aroma.js';
import path from 'path';
const app = new Aroma();
// Serve static files from the 'public' directory
app.serveStatic(path.join(__dirname, 'public'));
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Rate Limiting
import Aroma from 'aroma.js';
const app = new Aroma();
// define a rate limit
app.rateLimiter({ windowMs: 60000, max: 2 });
app.get('/', (req, res) => {
res.send({ message: 'Hello, world!' });
});
// Start server
app.listen(3000, () => {
console.log('Aroma.js server running on http://localhost:3000');
});