1.14.0 • Published 8 months ago
@cfworker/web v1.14.0
@cfworker/web
Web framework for Cloudflare Workers and service workers, inspired by Koa and fastify.
import {
Application,
Middleware,
Router,
validate,
normalizePathnameMiddleware
} from '@cfworker/web';
const router = new Router();
// Add the homepage route.
router.get('/', ({ res }) => {
res.body = `
<h1>@cfworker/web demo</h1>
<h2 id="example-routes">Example routes</h2>
<ol aria-labelledby="example-routes">
<li><a href="/greetings/hello">A valid greeting</a></li>
<li><a href="/greetings/hell">An invalid greeting</a></li>
<li><a href="/error">A route with a bug</a></li>
</ol>
<img src="favicon.ico" alt="cfworker logo">`;
});
// Add a greeting route with validation.
router.get(
'/greetings/:greeting',
validate({
params: {
required: ['greeting'],
properties: {
greeting: {
minLength: 5,
maxLength: 10
}
}
}
}),
({ req, res }) => {
res.body = req.params;
}
);
// Add a route to demonstrate exception handling.
router.get('/error', () => {
// @ts-ignore - this route has a bug!
req.this.method.does.not.exist();
});
// Favicon route for fun :)
router.get('/favicon.ico', ({ res }) => {
res.type = 'image/svg+xml';
res.body = `
<svg xmlns="http://www.w3.org/2000/svg" baseProfile="full" width="200" height="200">
<rect width="100%" height="100%" fill="#F38020"/>
<text font-size="120" font-family="Arial, Helvetica, sans-serif" text-anchor="end" fill="#FFF" x="185" y="185">W</text>
</svg>`;
});
// Simple CORS middleware.
const cors: Middleware = async ({ res }, next) => {
res.headers.set('access-control-allow-origin', '*');
await next();
};
// Compose the application
new Application()
.use(normalizePathnameMiddleware)
.use(cors)
.use(router.middleware)
.listen();
git clone https://github.com/cfworker/cfworker
cd cfworker
npm install
npm run start-web --workspace=@cfworker/examples
1.14.0
8 months ago
1.13.0
8 months ago
1.12.5
2 years ago
1.12.4
2 years ago
1.12.3
2 years ago
1.12.2
2 years ago
1.12.1
2 years ago
1.12.0
2 years ago
1.10.2
2 years ago
1.11.0
2 years ago
1.10.1
2 years ago
1.10.0
2 years ago
1.9.0
3 years ago
1.8.5
3 years ago
1.8.4
3 years ago
1.8.3
3 years ago
1.8.2
3 years ago
1.8.1
3 years ago
1.8.0
3 years ago
1.6.11
3 years ago
1.6.10
3 years ago
1.6.8
3 years ago
1.6.5
3 years ago
1.6.4
3 years ago
1.6.3
3 years ago
1.6.0
4 years ago
1.5.0
4 years ago
1.3.0
4 years ago
1.2.18
4 years ago
1.2.16
4 years ago
1.2.17
4 years ago
1.2.14
4 years ago
1.2.13
4 years ago
1.2.10
4 years ago
1.2.9
4 years ago
1.2.6
4 years ago
1.2.4
4 years ago
1.2.3
4 years ago
1.2.2
4 years ago
1.2.1
4 years ago
1.2.0
4 years ago
1.1.3
4 years ago
1.1.1
4 years ago
1.1.0
4 years ago
1.0.11
4 years ago
1.0.10
4 years ago
1.0.9
4 years ago
1.0.5
4 years ago