What is hono?
Hono is a small, simple, and fast web framework for building web applications and APIs in Node.js. It is designed to be lightweight and efficient, making it suitable for high-performance applications.
What are hono's main functionalities?
Basic Routing
Hono allows you to define routes for your web application. In this example, a basic GET route is defined that responds with 'Hello, Hono!' when accessed.
const { Hono } = require('hono');
const app = new Hono();
app.get('/', (c) => c.text('Hello, Hono!'));
app.listen(3000);
Middleware Support
Hono supports middleware, allowing you to execute code before your route handlers. This example demonstrates a simple logger middleware that logs the request method and URL.
const { Hono } = require('hono');
const app = new Hono();
const logger = (c, next) => {
console.log(`${c.req.method} ${c.req.url}`);
return next();
};
app.use(logger);
app.get('/', (c) => c.text('Hello, Hono!'));
app.listen(3000);
Error Handling
Hono provides a way to handle errors globally. In this example, an error is thrown in the route handler, and the global error handler responds with a 500 status code and a message.
const { Hono } = require('hono');
const app = new Hono();
app.get('/', (c) => {
throw new Error('Something went wrong!');
});
app.onError((err, c) => {
c.status(500);
return c.text('Internal Server Error');
});
app.listen(3000);
Other packages similar to hono
express
Express is a widely-used web framework for Node.js, known for its simplicity and flexibility. It offers a robust set of features for web and mobile applications, including routing, middleware support, and more. Compared to Hono, Express has a larger community and more extensive documentation, but Hono aims to be more lightweight and faster.
koa
Koa is a web framework designed by the team behind Express. It aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. Koa uses async functions to help eliminate callback hell and improve error handling. Compared to Hono, Koa is more modern and has a different approach to middleware, using a stack-like structure.
fastify
Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is designed for high performance and low overhead. Compared to Hono, Fastify is more feature-rich and has a more extensive ecosystem, but Hono aims to be simpler and more lightweight.
Hono
Hono [炎] - Tiny web framework for Cloudflare Workers and others.
const { Hono } = require('hono')
const app = new Hono()
app.get('/', () => new Response('Hono!!'))
app.fire()
Feature
- Fast - the router is implemented with Trie-Tree structure.
- Tiny - use only standard API.
- Portable - zero dependencies.
- Flexible - you can make your own middlewares.
- Optimized - for Cloudflare Workers and Fastly Compute@Edge.
Benchmark
hono x 813,001 ops/sec ±2.96% (75 runs sampled)
itty-router x 160,415 ops/sec ±3.31% (85 runs sampled)
sunder x 307,438 ops/sec ±4.77% (73 runs sampled)
Fastest is hono
✨ Done in 37.03s.
Install
$ yarn add hono
or
$ npm install hono
Methods
- app.HTTP_METHOD(path, callback)
- app.all(path, callback)
- app.route(path)
- app.use(path, middleware)
Routing
Basic
app.HTTP_METHOD
app.get('/', () => new Response('GET /'))
app.post('/', () => new Response('POST /'))
app.get('/wild/*/card', () => {
return new Response('GET /wild/*/card')
})
app.all
app.all('/hello', () => new Response('ALL Method /hello'))
Named Parameter
app.get('/user/:name', (c) => {
const name = c.req.params('name')
...
})
Regexp
app.get('/post/:date{[0-9]+}/:title{[a-z]+}', (c) => {
const date = c.req.params('date')
const title = c.req.params('title')
...
Chained Route
app
.route('/api/book')
.get(() => {...})
.post(() => {...})
.put(() => {...})
Middleware
Builtin Middleware
const { Hono, Middleware } = require('hono')
...
app.use('*', Middleware.poweredBy)
Custom Middleware
const logger = (c, next) => {
console.log(`[${c.req.method}] ${c.req.url}`)
next()
}
const addHeader = (c, next) => {
next()
c.res.headers.add('x-message', 'This is middleware!')
}
app.use('*', logger)
app.use('/message/*', addHeader)
app.get('/message/hello', () => 'Hello Middleware!')
Custom 404 Response
const customNotFound = (c, next) => {
next()
if (c.res.status === 404) {
c.res = new Response('Custom 404 Not Found', { status: 404 })
}
}
app.use('*', customNotFound)
Context
req
app.get('/hello', (c) => {
const userAgent = c.req.headers('User-Agent')
...
})
res
app.use('/', (c, next) => {
next()
c.res.headers.append('X-Debug', 'Debug message')
})
Request
query
app.get('/search', (c) => {
const query = c.req.query('q')
...
})
params
app.get('/entry/:id', (c) => {
const id = c.req.params('id')
...
})
Related projects
Author
Yusuke Wada https://github.com/yusukebe
License
MIT