Skip to content

omraval18/mini-express

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mini-express

A lightweight Express-compatible framework built from scratch with Node.js core modules.

Features

  • app.listen(port, cb) over Node http
  • Routing: get, post, put, patch, delete
  • Middleware system with app.use(...) and next()
  • Route-specific middleware (app.get('/admin', auth, handler))
  • Dynamic route params (/users/:id/books/:bookId)
  • Request helpers: req.params, req.query, req.body
  • Response helpers: res.status, res.send, res.json, res.redirect
  • Async handler support with automatic promise rejection forwarding
  • Centralized error middleware
  • Nested routers (mini.Router() + app.use('/prefix', router))
  • Static file middleware (mini.static(dir))
  • JSON body parser (mini.json())
  • Next.js-like file-based API routing (app.useFileRoutes, mini.fileRoutes)
  • Built-in 404 JSON response
  • TypeScript declarations (index.d.ts)

Install

npm install

Quick start

const mini = require('./index');

const app = mini();

app.use(mini.json());

app.get('/', (req, res) => {
  res.send('Hello from Mini Express');
});

app.get('/users/:id', (req, res) => {
  res.json({ id: req.params.id });
});

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

File-based API routes

examples/api/users/[id].js maps to GET /api/users/:id.

const path = require('node:path');
const mini = require('./index');

const app = mini();
app.useFileRoutes(path.join(__dirname, 'api'), { basePath: '/api' });

Supported conventions:

  • index.js => directory root route
  • [id].js => dynamic param :id
  • [...slug]/index.js => catch-all param :slug*
  • named exports: get, post, put, patch, delete

Scripts

  • npm test - run unit tests
  • npm run typecheck - verify TypeScript usage
  • npm start - run examples/basic.js

Examples

  • examples/basic.js
  • examples/router.js
  • examples/auth.js
  • examples/rest-api.js
  • examples/file-routes.js
  • examples/typescript.ts

Notes

This project intentionally keeps implementation simple and readable while following good structure and naming conventions.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors