@whatwg-node/server 0.5.5 → 0.5.6-alpha-20230111213850-946da63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -22
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -242,16 +242,14 @@ You can learn more about [File API](https://developer.mozilla.org/en-US/docs/Web
|
|
|
242
242
|
|
|
243
243
|
## Routing and Middlewares
|
|
244
244
|
|
|
245
|
-
We'd recommend to use
|
|
245
|
+
We'd recommend to use `@whatwg-node/router` to handle routing and middleware approach. It uses `@whatwg-node/server` under the hood.
|
|
246
246
|
|
|
247
247
|
### Basic Routing
|
|
248
248
|
|
|
249
249
|
```ts
|
|
250
|
-
import { Router } from '
|
|
251
|
-
import { createServerAdapter } from '@whatwg-node/server'
|
|
250
|
+
import { createRouter, Router } from '@whatwg-node/router'
|
|
252
251
|
|
|
253
|
-
|
|
254
|
-
const router = Router()
|
|
252
|
+
const router = createRouter()
|
|
255
253
|
// GET collection index
|
|
256
254
|
router.get('/todos', () => new Response('Todos Index!'))
|
|
257
255
|
// GET item
|
|
@@ -268,21 +266,18 @@ router.get('/google', () => Response.redirect('http://www.google.com'))
|
|
|
268
266
|
// 404 for everything else
|
|
269
267
|
router.all('*', () => new Response('Not Found.', { status: 404 }))
|
|
270
268
|
|
|
271
|
-
// attach the router to our server adapter
|
|
272
|
-
const myServerAdapter = createServerAdapter(router)
|
|
273
|
-
|
|
274
269
|
// Then use it in any environment
|
|
275
270
|
import { createServer } from 'http'
|
|
276
|
-
const httpServer = createServer(
|
|
271
|
+
const httpServer = createServer(router)
|
|
277
272
|
httpServer.listen(4000)
|
|
278
273
|
```
|
|
279
274
|
|
|
280
275
|
### Middlewares to handle CORS, cookies and more
|
|
281
276
|
|
|
282
|
-
|
|
277
|
+
This package also provides some utilities for your platform agnostic server implementation. The following example shows how to get the cookies as an object from the request.
|
|
283
278
|
|
|
284
279
|
```ts
|
|
285
|
-
import { withCookies } from '
|
|
280
|
+
import { withCookies } from '@whatwg-node/server'
|
|
286
281
|
|
|
287
282
|
router.get('/foo', withCookies, ({ cookies }) => {
|
|
288
283
|
// cookies are parsed from the header into request.cookies
|
|
@@ -293,15 +288,12 @@ router.get('/foo', withCookies, ({ cookies }) => {
|
|
|
293
288
|
You can also setup a CORS middleware to handle preflight CORS requests.
|
|
294
289
|
|
|
295
290
|
```ts
|
|
296
|
-
import { withCors } from '
|
|
297
|
-
|
|
298
|
-
router
|
|
299
|
-
'
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
credentials: false
|
|
305
|
-
})
|
|
306
|
-
)
|
|
291
|
+
import { withCors } from '@whatwg-node/server'
|
|
292
|
+
|
|
293
|
+
const corsWithRouter = withCors(router, {
|
|
294
|
+
origin: 'http://localhost:4000',
|
|
295
|
+
methods: 'GET, POST, PATCH, DELETE',
|
|
296
|
+
headers: 'authorization, referer, origin, content-type',
|
|
297
|
+
credentials: false
|
|
298
|
+
})
|
|
307
299
|
```
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whatwg-node/server",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.6-alpha-20230111213850-946da63",
|
|
4
4
|
"description": "Fetch API compliant HTTP Server adapter",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"@types/node": "^18.0.6"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@whatwg-node/fetch": "0.6.
|
|
10
|
+
"@whatwg-node/fetch": "0.6.2-alpha-20230111213850-946da63",
|
|
11
11
|
"tslib": "^2.3.1"
|
|
12
12
|
},
|
|
13
13
|
"repository": {
|