Basic Router Middleware API Endpoints Error Handling

Eiseniiiia Framework Examples

Discover how to build powerful applications with code examples covering routing, middleware, API endpoints, and more.

1. Basic Router Setup


import { Router } from 'eiseniiiia';

const app = Router();

app.get('/', (req, res) => {
  res.send('Welcome to the home page!');
});

app.get('/about', (req, res) => {
  res.send('About page content here.');
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Fundamental routing with GET endpoints

How It Works

This example demonstrates creating a simple router with two endpoints. The route handler functions receive request and response objects, allowing you to send responses directly.

GET /
GET /about

2. Middleware Implementation


function loggingMiddleware(req, res, next) {
  console.log(`${req.method} request to ${req.url}`);
  next();
}

app.get('/data', loggingMiddleware, (req, res) => {
  res.json({ success: true, data: 'Secret info' });
});

Real-time request logging middleware
Request Flow
Request
Middleware
Route Handler

3. RESTful API Endpoints


app.get('/api/users', (req, res) => {
  res.json([
    { id: 1, name: "Alice" },
    { id: 2, name: "Bob" }
  ]);
});

app.post('/api/users', (req, res) => {
  const newUser = req.body;
  // Add to database logic here
  res.status(201).json(newUser);
});

GET and POST API endpoint examples

Try the API

curl -X GET http://localhost:3000/api/users
HTTP/1.1 200 OK 224 B
[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]
                            

4. Error Handling


app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({
    error: "Something broke!",
    details: process.env.NODE_ENV === "development" ? err.message : undefined
  });
});

Production-ready error handling
Error Flow
Error
Handler

5. Advanced Examples

File Uploads with Streaming

Implement multipart form parsing and file storage using built-in streaming capabilities.

/examples/upload

WebSocket Integration

Build real-time applications with bidirectional communication using Eiseniiiia's WebSocket API.

/examples/socket