Skip to content

Commit

Permalink
docs: used @whatwg-node/server instead of srvx
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Nov 19, 2024
1 parent 2c1f435 commit 50cc352
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 81 deletions.
36 changes: 21 additions & 15 deletions apps/content/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,33 +137,39 @@ In oRPC middleware is very useful and fully typed you can find more info [here](

## Start Your Server

This example uses [srvx](https://srvx.unjs.io/) to create a Node server with [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
This example uses [@whatwg-node/server](https://www.npmjs.com/package/@whatwg-node/server) to create a Node server with [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).

```ts twoslash
import { createFetchHandler } from '@orpc/server/fetch'
import { serve } from 'srvx'
import { createServer } from 'node:http'
import { createServerAdapter } from '@whatwg-node/server'
import { router } from 'examples/server'

const handler = createFetchHandler({
router,
serverless: false, // set true will improve cold start times
})

const server = serve({
fetch(request) {
return handler({
request,
context: {},
prefix: '/api'
})
},
port: 2206,
})
const server = createServer(
createServerAdapter((request: Request) => {
const url = new URL(request.url)

await server.ready()
if (url.pathname.startsWith('/api')) {
return handler({
request,
prefix: '/api',
context: {},
})
}

// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(`🚀 Server ready at ${server.url}`)
return new Response('Not found', { status: 404 })
}),
)

server.listen(2026, () => {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log('Server is available at http://localhost:2026')
})
```

Start the server and visit http://localhost:2026/api/getting?name=yourname to see the result.
Expand Down
34 changes: 20 additions & 14 deletions apps/content/content/docs/server/integrations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ so you can use it in any modern environment and framework.

```ts twoslash
import { createFetchHandler } from '@orpc/server/fetch'
import { serve } from 'srvx'
import { createServer } from 'node:http'
import { createServerAdapter } from '@whatwg-node/server'
import { router } from 'examples/server'

const handler = createFetchHandler({
Expand All @@ -19,21 +20,26 @@ const handler = createFetchHandler({
})


const server = serve({
fetch(request) {
return handler({
request,
context: {},
prefix: '/api',
})
},
port: 2206,
})
const server = createServer(
createServerAdapter((request: Request) => {
const url = new URL(request.url)

await server.ready()
if (url.pathname.startsWith('/api')) {
return handler({
request,
prefix: '/api',
context: {},
})
}

// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(`🚀 Server ready at ${server.url}`)
return new Response('Not found', { status: 404 })
}),
)

server.listen(2026, () => {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log('Server is available at http://localhost:2026')
})
```

Under the hood, oRPC utilizes the RegExpRouter from [Hono](https://hono.dev/) (the fastest router in the JavaScript ecosystem).
Expand Down
36 changes: 21 additions & 15 deletions apps/content/examples/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,29 @@ const handler = createFetchHandler({

// Modern runtime that support fetch api like deno, bun, cloudflare workers, even node can used

import { serve } from 'srvx'

const server = serve({
fetch(request) {
return handler({
request,
context: {},
prefix: '/api',
})
},
port: 2206,
})
import { createServer } from 'node:http'
import { createServerAdapter } from '@whatwg-node/server'

const server = createServer(
createServerAdapter((request: Request) => {
const url = new URL(request.url)

if (url.pathname.startsWith('/api')) {
return handler({
request,
prefix: '/api',
context: {},
})
}

await server.ready()
return new Response('Not found', { status: 404 })
}),
)

// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(`🚀 Server ready at ${server.url}`)
server.listen(2026, () => {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log('Server is available at http://localhost:2026')
})

//
//
36 changes: 21 additions & 15 deletions apps/content/examples/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,29 @@ const handler = createFetchHandler({

// Modern runtime that support fetch api like deno, bun, cloudflare workers, even node can used

import { serve } from 'srvx'

const server = serve({
fetch(request) {
return handler({
request,
context: {},
prefix: '/api',
})
},
port: 2206,
})
import { createServer } from 'node:http'
import { createServerAdapter } from '@whatwg-node/server'

const server = createServer(
createServerAdapter((request: Request) => {
const url = new URL(request.url)

if (url.pathname.startsWith('/api')) {
return handler({
request,
prefix: '/api',
context: {},
})
}

await server.ready()
return new Response('Not found', { status: 404 })
}),
)

// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(`🚀 Server ready at ${server.url}`)
server.listen(2026, () => {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log('Server is available at http://localhost:2026')
})

//
//
8 changes: 4 additions & 4 deletions apps/content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
"geist": "^1.3.1",
"next": "15.0.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"srvx": "^0.1.1",
"zod": "^3.23.8"
"react-dom": "^18.3.1"
},
"devDependencies": {
"@orpc/client": "workspace:*",
Expand All @@ -33,9 +31,11 @@
"@types/node": "22.9.0",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@whatwg-node/server": "^0.9.55",
"autoprefixer": "^10.4.20",
"postcss": "^8.4.48",
"tailwindcss": "^3.4.14",
"typescript": "^5.6.3"
"typescript": "^5.6.3",
"zod": "^3.23.8"
}
}
Loading

0 comments on commit 50cc352

Please sign in to comment.