11# h2
22
3- [ ![ npm] ( https://img.shields.io/npm/dm/h-2 .svg?style=flat-square )] ( https://npmjs.com/package/h-2 )
4- [ ![ npm (scoped with tag)] ( https://img.shields.io/npm/v/h-2/ latest.svg?style=flat-square )] ( https://npmjs.com/package/h-2 )
3+ [ ![ npm] ( https://img.shields.io/npm/dm/@nuxt/h2 .svg?style=flat-square )] ( https://npmjs.com/package/@nuxt/h2 )
4+ [ ![ npm (scoped with tag)] ( https://img.shields.io/npm/v/@nuxt/h2/ latest.svg?style=flat-square )] ( https://npmjs.com/package/@nuxt/h2 )
55
66> h2 is a 3 kB modern and environment agnostic http framework
77
1111- Compatibile with connect/express middleware
1212- Tree-shakable and zero dependency
1313- Promise and ` aync/await ` support
14- - Lazy loading middleware
15- - Basic router
14+ - Lazy loading
15+ - Basic Router
1616
1717> See [ un] ( https://github.com/nuxt-contrib/un ) for workers support
1818
@@ -31,13 +31,14 @@ const { Server } = require('http')
3131const { createApp } = require (' @nuxt/h2' )
3232
3333const app = createApp ()
34- app . use ( ' / ' , () => ' Hello world! ' )
34+
3535app .use (' /api' , (req ) => ({ url: req .url }))
36+ app .use (' /' , () => ' Hello world!' )
3637
3738const port = process .env .PORT || 3000
3839const server = new Server (app .handle )
3940server .listen (port, () => {
40- console .log (` Listening on: http://localhost:${ port} `
41+ console .log (` Listening on: http://localhost:${ port} ` )
4142})
4243```
4344
@@ -48,44 +49,6 @@ server.listen(port, () => {
4849- ` redirect (req, location, code) `
4950- ` lazy (handle) `
5051
51- ## ` promisifyHandle`
52-
53- Optionally you can use express or your own middleware system by directly using this utility and not using app stack.
54-
55- Converts handle/middleware into a promisified async version:
56-
57- - Ensures if handle is an ` async ` function or returning ` Promise ` , error is passed to the next middleware
58- - If middleware supports a third param (` next` callback) it will be piped to the promise chain
59- - If middleware returns or calls ` next (null , value)` it will be piped to the promise chain
60- - Otherwise resolves promise to ` response .writableEnded ` to indicate response is ended or not
61-
62- **Why?**
63-
64- - Avoid unhandled promise rejections since developers usually create ` async ` middleware without handling errors
65- - Allow directly using any middleware (like ` serve- static ` ) by awaiting on it
66- - Convert vanilla http server into a modern handler supporting async and json!
67-
68- **Example:** Register middleware and ensure it is safe using async operations
69-
70- ` ` ` js
71- const express = require (' express' )
72-
73- const app = express ()
74-
75- const test = async (req , res ) => {
76- await Promise .reject (new Error (' oops!' ))
77- }
78-
79- app .use (' /api/test' , promisifyHandle (test))
80- ` ` `
81-
82- **Example:** Use ` serve- static ` as an awaitable utility
83-
84- ` ` ` js
85- const serveStatic = promisifyHandle (require (' serve-static' ))
86- const served = await serveStatic (req, res)
87- ` ` `
88-
8952## License
9053
9154MIT
0 commit comments