What is @tinyhttp/app?
@tinyhttp/app is a modern, lightweight, and fast web framework for Node.js. It is designed to be a minimalistic alternative to Express.js, providing a similar API but with a smaller footprint and faster performance.
What are @tinyhttp/app's main functionalities?
Basic Routing
This code demonstrates how to set up a basic HTTP GET route using @tinyhttp/app. When a GET request is made to the root URL, the server responds with 'Hello, World!'.
const { App } = require('@tinyhttp/app');
const app = new App();
app.get('/', (req, res) => res.send('Hello, World!'));
app.listen(3000, () => console.log('Server is running on http://localhost:3000'));
Middleware Support
This code demonstrates how to use middleware in @tinyhttp/app. The logger middleware logs the HTTP method and URL of each request before passing control to the next middleware or route handler.
const { App } = require('@tinyhttp/app');
const app = new App();
const logger = (req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
};
app.use(logger);
app.get('/', (req, res) => res.send('Hello, World!'));
app.listen(3000, () => console.log('Server is running on http://localhost:3000'));
Error Handling
This code demonstrates how to handle errors in @tinyhttp/app. If an error is thrown in a route handler, the error-handling middleware catches it and sends a 500 Internal Server Error response.
const { App } = require('@tinyhttp/app');
const app = new App();
app.get('/', (req, res) => {
throw new Error('Something went wrong!');
});
app.use((err, req, res, next) => {
res.status(500).send('Internal Server Error');
});
app.listen(3000, () => console.log('Server is running on http://localhost:3000'));
Other packages similar to @tinyhttp/app
express
Express is a widely-used web framework for Node.js, known for its robust features and extensive middleware ecosystem. Compared to @tinyhttp/app, Express has a larger community and more plugins, but it is also heavier and may have slower performance.
koa
Koa is a web framework designed by the creators of Express. It uses async functions and promises to provide a more modern and elegant API. Koa is more modular and lightweight than Express, but it requires more boilerplate code compared to @tinyhttp/app.
fastify
Fastify is a high-performance web framework for Node.js, designed for speed and low overhead. It offers a schema-based validation system and a powerful plugin architecture. Fastify is similar to @tinyhttp/app in terms of performance but has a different API and feature set.
tinyhttp
β The project is in development. Please don't use in production.
tinyhttp is a modern Express-like web framework for Node.js. It uses a bare minimum amount of dependencies trying to avoid legacy hell.
Here is a short list of most important features that tinyhttp has:
- β Full Express middleware support
- βͺ Async middleware support
- π¦ 8x smaller than Express
- π No legacy dependencies
- π¨ Types out of the box
- β Native ESM and CommonJS support
To get started, visit tinyhttp website.
Install
Node.js 12.4.0 or newer is required.
npm i @tinyhttp/app
pnpm i @tinyhttp/app
yarn add @tinyhttp/app
Docs
You can see the documentation here.
Example
import { App } from '@tinyhttp/app'
import logger from '@tinyhttp/logger'
const app = new App()
app
.use(function someMiddleware(req, res, next) {
console.log('Did a request')
next()
})
.get('/', (_, res) => {
res.send('<h1>Hello World</h1>')
})
.get('/page/:page/', (req, res) => {
res.status(200).send(`${JSON.stringify(req.params, null, 2)}`)
})
.use(logger())
.listen(3000)
For more examples check examples folder.
Middlewares
tinyhttp offers a list of premade middleware for common tasks.
Search and explore the full list at middleware search page.
License
MIT Β© v1rtl