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/core
tinyhttp
core module with App
, Request
and Response
classes.
⚠ The project is incomplete. 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.
Installation
Node.js 13 is required.
npm i @tinyhttp/app
pnpm i @tinyhttp/app
yarn add @tinyhttp/app
Features
- Compatible with Express
- Async routes [not tested yet]
- Smaller size
- 0 legacy dependencies
Docs
Coming soon...
Example
At the moment there is only one basic example. I will add more of them once I add all the existing Express req
/ res
extensions.
import { App } from '@tinyhttp/app'
import staticFolder from '@tinyhttp/static'
import logger from '@tinyhttp/logger'
const app = new App()
app.all('/', (req, res) => {
res.status(200).send(`
<h1>tinyhttp example</h1>
<ul>
<li>Protocol: ${req.protocol}</li>
<li>HTTPS: ${req.secure ? 'yes' : 'no'}</li>
<li>URL: ${req.url}</li>
<li>Method: ${req.method}</li>
<li>Host: ${req.hostname}</li>
<li>Status: ${res.statusCode}</li>
</ul>
<h2>Request headers</h2>
<pre>
${JSON.stringify(req.headers, null, 2)}
</pre>
`)
})
app.get('/:first/:second', (req, res) => {
res.json({ URLParams: req.params, QueryParams: req.query })
})
app.use(staticFolder())
app.use(logger())
app.listen(3000)