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/app
The core of tinyhttp. Contains the App
, Request
and Response
. Additionally, it provides special tinyhttp-specific types.
Install
pnpm i @tinyhttp/app
Example
import { App } from '@tinyhttp/app'
import type { Request, Response, NextFunction } from '@tinyhttp/app'
new App()
.use((req: Request, res: Response, next: NextFunction) => {
console.log('Did a request')
next()
})
.get('/', (_, res) => res.send('<h1>Hello World</h1>'))
.get('/page/:page', (req, res) => res.send(`You opened ${req.params.page}`))
.listen(3000)