Package Exports
- h3
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (h3) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
H3 is a minimal h(ttp) framework built for high performance and portability
Features
✔️ Portable: Works perfectly in Serverless, Workers, and Node.js
✔️ Compatible: Support connect/express middleware
✔️ Minimal: Small, tree-shakable and zero-dependency
✔️ Modern: Native promise support
✔️Extendable: Ships with a set of composable utilities but can be extended
Install
# Using npm
npm install h3
# Using yarn
yarn add h3
Usage
import { createServer } from 'http'
import { createApp } from 'h3'
const app = createApp()
app.useAsync('/', () => 'Hello world!')
listen(app)
Tip: you may try listhen for a more elegant and advanced listener.
Examples
// Handle can directly return object or Promise<object> for JSON response
app.useAsync('/api', (req) => ({ url: req.url }))
// We can have better matching other than quick prefix match
app.useAsync('/odd', () => 'Is odd!', { match: url => url.substr(1) % 2 })
// Handle can directly return string for HTML response
app.useAsync(() => '<h1>Hello world!</h1>')
// We can chain calls to .use()
app.useAsync('/1', () => '<h1>Hello world!</h1>')
.useAsync('/2', () => '<h1>Goodbye!</h1>')
// Promisify middleware before register: (supporting (req, res, next) format)
// app.use(async () => {})
// Lazy loaded routes using { lazy: true }
// app.use('/big', () => import('./big'), { lazy: true })
Utilities
Instead of adding helpers to req
and res
, h3 exposes them as composable utilities.
useRawBody(req, encoding?)
useBody(req)
useCookies(req)
useCookie(req, name)
setCookie(req, name, value, opts?)
useQuery(req)
send(res, data, type?)
sendRedirect(res, location, code=302)
appendHeader(res, name, value)
createError({ statusCode, statusMessage, data? }
sendError(res, error, debug?)
How it works?
Using createApp
, it returns a standard (req, res)
handler function and internally an array called middleware stack. useAsync()
and use()
methods are helpers to add an item to this internal stack.
When a request comes, each stack item that matches the route will be called and resolved until res.writableEnded
flag is set, which means the response is sent. If writableEnded
is not set after all middleware, a 404
error will be thrown. And if one of the stack items resolves to a value, it will be serialized and sent as response as a shorthand method to sending responses.
For maximum compatibility with connect/express middleware (req, res, next?
signature), when using use
instead of useAsync
, it converts classic middleware into a promisified version ready to use with stack runner:
- If middleware has 3rd next/callback param, promise will
resolve/reject
when called - If middleware returns a promise, it will be chained to the main promise
- If calling middleware throws an immediate error, promise will be rejected
- On
close
anderror
events of res, promise willresolve/reject
(to ensure if middleware simply callsres.end
)
License
MIT