Skip to content

Commit

Permalink
docs(node-utils): improves the example
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed Dec 19, 2022
1 parent 6508a07 commit 3008c78
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions docs/pages/packages/node-utils.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ This package includes built-in TypeScript support.

## Usage

```js
```ts
import { once } = from 'node:events'
import { createServer } from 'node:http'
import { buildToNodeHandler } from '@edge-runtime/node-utils'

Expand All @@ -30,19 +31,21 @@ const transformToNode = buildToNodeHandler(global, {
const server = await createServer(
// 2. takes an web compliant request handler, that uses Web globals like Request and Response,
// and turn it into a Node.js compliant request handler.
transformToNode((req) => new Response(req.body))
transformToNode(async (req: Request) => new Response(req.body))
)

// 3. start the node.js server
await server.listen()
server.listen()
await once(server, 'listening')

// 4. invoke the request handler
const response = await fetch(`http://localhost:${server.address().port}`, {
method: 'POST',
body: 'hello world',
})
const response = await fetch(
`http://localhost:${(server.address() as AddressInfo).port}`,
{ method: 'POST', body: 'hello world' }
)

console.log(await response.text()) // is 'hello world'
await server.close()
```

## API
Expand Down

0 comments on commit 3008c78

Please sign in to comment.