Update README.md
This commit is contained in:
78
README.md
78
README.md
@@ -32,14 +32,14 @@ I worked a lot with [Express.js](https://expressjs.com/) in the past and really
|
||||
import { Router } from '@tsndr/cloudflare-worker-router'
|
||||
|
||||
export interface Env {
|
||||
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
||||
// MY_KV_NAMESPACE: KVNamespace;
|
||||
//
|
||||
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
|
||||
// MY_DURABLE_OBJECT: DurableObjectNamespace;
|
||||
//
|
||||
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
||||
// MY_BUCKET: R2Bucket;
|
||||
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
||||
// MY_KV_NAMESPACE: KVNamespace;
|
||||
//
|
||||
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
|
||||
// MY_DURABLE_OBJECT: DurableObjectNamespace;
|
||||
//
|
||||
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
||||
// MY_BUCKET: R2Bucket;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,59 +51,59 @@ router.cors()
|
||||
|
||||
// Register global middleware
|
||||
router.use(({ req, res, next }) => {
|
||||
res.headers.set('X-Global-Middlewares', 'true')
|
||||
next()
|
||||
res.headers.set('X-Global-Middlewares', 'true')
|
||||
next()
|
||||
})
|
||||
|
||||
// Simple get
|
||||
router.get('/user', ({ req, res }) => {
|
||||
res.body = {
|
||||
data: {
|
||||
id: 1,
|
||||
name: 'John Doe'
|
||||
res.body = {
|
||||
data: {
|
||||
id: 1,
|
||||
name: 'John Doe'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Post route with url parameter
|
||||
router.post('/user/:id', ({ req, res }) => {
|
||||
|
||||
const userId = req.params.id
|
||||
|
||||
// Do stuff...
|
||||
|
||||
if (errorDoingStuff) {
|
||||
res.status = 400
|
||||
res.body = {
|
||||
error: 'User did stupid stuff!'
|
||||
const userId = req.params.id
|
||||
|
||||
// Do stuff...
|
||||
|
||||
if (errorDoingStuff) {
|
||||
res.status = 400
|
||||
res.body = {
|
||||
error: 'User did stupid stuff!'
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
res.status = 204
|
||||
|
||||
res.status = 204
|
||||
})
|
||||
|
||||
// Delete route using a middleware
|
||||
router.delete('/user/:id', ({ req, res, next }) => {
|
||||
|
||||
if (!apiTokenIsCorrect) {
|
||||
res.status = 401
|
||||
return
|
||||
}
|
||||
|
||||
await next()
|
||||
if (!apiTokenIsCorrect) {
|
||||
res.status = 401
|
||||
return
|
||||
}
|
||||
|
||||
await next()
|
||||
}, (req, res) => {
|
||||
|
||||
const userId = req.params.id
|
||||
|
||||
// Do stuff...
|
||||
const userId = req.params.id
|
||||
|
||||
// Do stuff...
|
||||
})
|
||||
|
||||
// Listen Cloudflare Workers Fetch Event
|
||||
export default {
|
||||
async fetch(request, env) {
|
||||
return router.handle(env, request)
|
||||
}
|
||||
async fetch(request: Request, env: Env): Promise<Response> {
|
||||
return router.handle(env, request)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user