update documentation
This commit is contained in:
45
README.md
45
README.md
@@ -1,7 +1,11 @@
|
|||||||
# Cloudflare Workers Router
|
# Cloudflare Workers Router
|
||||||
|
|
||||||
---
|
---
|
||||||
**PRE-RELEASE: This branch is only for use with [wrangler2](https://github.com/cloudflare/wrangler2)!**
|
### **PRE-RELEASE**
|
||||||
|
|
||||||
|
**This branch is only for use with [wrangler2](https://github.com/cloudflare/wrangler2) and might also not work reliably!**
|
||||||
|
|
||||||
|
**USE AT YOUR OWN RISK!**
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -11,6 +15,7 @@ When I was trying out Cloudflare Workers I almost immediately noticed how fast i
|
|||||||
|
|
||||||
I worked a lot with [Express.js](https://expressjs.com/) in the past and really enjoyed their middleware approach, but since none of the available Cloudflare Worker routers offered middleware support at the time, I felt the need to create this router.
|
I worked a lot with [Express.js](https://expressjs.com/) in the past and really enjoyed their middleware approach, but since none of the available Cloudflare Worker routers offered middleware support at the time, I felt the need to create this router.
|
||||||
|
|
||||||
|
|
||||||
## Contents
|
## Contents
|
||||||
|
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
@@ -23,20 +28,22 @@ I worked a lot with [Express.js](https://expressjs.com/) in the past and really
|
|||||||
### Simple Example
|
### Simple Example
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const Router = require('@tsndr/cloudflare-worker-router')
|
import Router from '@tsndr/cloudflare-worker-router'
|
||||||
|
|
||||||
|
// Initialize router
|
||||||
const router = new Router()
|
const router = new Router()
|
||||||
|
|
||||||
// Enabling buildin CORS support
|
// Enabling buildin CORS support
|
||||||
router.cors()
|
router.cors()
|
||||||
|
|
||||||
// Register global middleware
|
// Register global middleware
|
||||||
router.use((req, res, next) => {
|
router.use(({ req, res, next }) => {
|
||||||
res.headers.set('X-Global-Middlewares', 'true')
|
res.headers.set('X-Global-Middlewares', 'true')
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
// Simple get
|
// Simple get
|
||||||
router.get('/user', (req, res) => {
|
router.get('/user', ({ req, res }) => {
|
||||||
res.body = {
|
res.body = {
|
||||||
data: {
|
data: {
|
||||||
id: 1,
|
id: 1,
|
||||||
@@ -46,7 +53,7 @@ router.get('/user', (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Post route with url parameter
|
// Post route with url parameter
|
||||||
router.post('/user/:id', (req, res) => {
|
router.post('/user/:id', ({ req, res }) => {
|
||||||
|
|
||||||
const userId = req.params.id
|
const userId = req.params.id
|
||||||
|
|
||||||
@@ -64,7 +71,7 @@ router.post('/user/:id', (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Delete route using a middleware
|
// Delete route using a middleware
|
||||||
router.delete('/user/:id', (req, res, next) => {
|
router.delete('/user/:id', ({ req, res, next }) => {
|
||||||
|
|
||||||
if (!apiTokenIsCorrect) {
|
if (!apiTokenIsCorrect) {
|
||||||
res.status = 401
|
res.status = 401
|
||||||
@@ -80,9 +87,11 @@ router.delete('/user/:id', (req, res, next) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Listen Cloudflare Workers Fetch Event
|
// Listen Cloudflare Workers Fetch Event
|
||||||
addEventListener('fetch', event => {
|
export default {
|
||||||
event.respondWith(router.handle(event.request))
|
async fetch(request, env, ctx) {
|
||||||
})
|
return router.handle(env, request)
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@@ -141,6 +150,15 @@ Supports the use of dynamic parameters, prefixed with a `:` (i.e. `/user/:userId
|
|||||||
An unlimited number of functions getting [`req`](#req-object) and [`res`](#res-object) passed into them.
|
An unlimited number of functions getting [`req`](#req-object) and [`res`](#res-object) passed into them.
|
||||||
|
|
||||||
|
|
||||||
|
### `ctx`-Object
|
||||||
|
Key | Type | Description
|
||||||
|
--------- | ------------------- | -----------
|
||||||
|
`env` | `object` | Environment
|
||||||
|
`req` | `req`-Object | Request Object
|
||||||
|
`res` | `res`-Object | Response Object
|
||||||
|
`next` | `next`-Handler | Next Handler
|
||||||
|
|
||||||
|
|
||||||
### `req`-Object
|
### `req`-Object
|
||||||
|
|
||||||
Key | Type | Description
|
Key | Type | Description
|
||||||
@@ -151,6 +169,7 @@ Key | Type | Description
|
|||||||
`params` | `object` | Object containing all parameters defined in the url string
|
`params` | `object` | Object containing all parameters defined in the url string
|
||||||
`query` | `object` | Object containing all query parameters
|
`query` | `object` | Object containing all query parameters
|
||||||
|
|
||||||
|
|
||||||
### `res`-Object
|
### `res`-Object
|
||||||
|
|
||||||
Key | Type | Description
|
Key | Type | Description
|
||||||
@@ -167,6 +186,7 @@ Key | Type | Description
|
|||||||
|
|
||||||
You can use [wrangler2](https://github.com/cloudflare/wrangler2) to generate a new Cloudflare Workers project based on this router by running the following command from your terminal:
|
You can use [wrangler2](https://github.com/cloudflare/wrangler2) to generate a new Cloudflare Workers project based on this router by running the following command from your terminal:
|
||||||
|
|
||||||
|
// TODO: Needs update!
|
||||||
```
|
```
|
||||||
wrangler generate myapp https://github.com/tsndr/cloudflare-worker-router-template
|
wrangler generate myapp https://github.com/tsndr/cloudflare-worker-router-template
|
||||||
```
|
```
|
||||||
@@ -191,10 +211,5 @@ wrangler dev
|
|||||||
If you already have a wrangler project you can install the router like this:
|
If you already have a wrangler project you can install the router like this:
|
||||||
|
|
||||||
```
|
```
|
||||||
npm i @tsndr/cloudflare-worker-router
|
npm i @tsndr/cloudflare-worker-router@pre
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Serverless
|
|
||||||
|
|
||||||
To deploy using serverless add a [`serverless.yml`](https://serverless.com/framework/docs/providers/cloudflare/) file.
|
|
||||||
|
|||||||
Reference in New Issue
Block a user