npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-cloudflare-workers-adapter

v0.0.4

Published

This adapter bridges the gap between Koa and Cloudflare Workers. With minimal code changes, you can leverage your existing Koa application logic within a Cloudflare Worker environment.

Downloads

248

Readme

This adapter bridges the gap between Koa and Cloudflare Workers. With minimal code changes, you can leverage your existing Koa application logic within a Cloudflare Worker environment.

Benefits

  • Seamless Integration: Easily run your Koa application in Cloudflare Workers.
  • Reduced Development Time: Leverage existing Koa codebase without significant modifications.
  • Improved Performance: Enjoy the benefits of Cloudflare's global edge network.

Installation

  1. Install the adapter package in your existing Koa project:
npm install koa-cloudflare-workers-adapter --save

Usage

1. Prepare Your Koa Project:

Existing Project: If you have an existing Koa project, you can skip this step.

New Project: Here's a basic Koa application structure for reference:

import koa from 'koa';
import Router from '@koa/router';
import { bodyParser }from '@koa/bodyparser';

const app = new koa();
const parser = bodyParser();
const router = new Router();

app.use(parser);

// Define your Koa routes here (example routes below)

router.get('/', async (ctx) => {
  ctx.body = 'Default';
});

router.get('/test', async (ctx) => {
  ctx.body = 'Test handler';
});

router.post('/handlepost', async (ctx) => {
  ctx.body = ctx.request.body;
});

app.use(router.routes());

export {
  app,
};

2. Include the Adapter:

Install the koa-cloudflare-workers-adapter package in your Koa project.

3. Integrate with Cloudflare Workers:

In your Cloudflare Worker's index.js or index.ts file:

import { KoaCloudflareWorkersAdapter } from 'koa-cloudflare-workers-adapter';
import { app } from './your-koa-app'; // Replace with your Koa app import path

export default {
  fetch: new KoaCloudflareWorkersAdapter(app).fetch,
};

Explanation:

  • We import the KoaCloudflareWorkersAdapter class.
  • We import your Koa application instance (app).
  • We create a new adapter instance using your Koa app.
  • We access the fetch function from the adapter and assign it to the fetch property of the default export object.

This configuration routes incoming Cloudflare Worker requests to your Koa application's middleware chain for processing.

Optional Customization:

The example provided utilizes the default behavior of the adapter. You can optionally customize the fetch function for more control over request handling within the Cloudflare Worker environment.

Additional Notes:

  • Ensure your Koa project includes all necessary dependencies (e.g., koa, @koa/router, etc.).
  • Refer to the Koa documentation for further details on middleware and routing: https://github.com/koajs/koa

Contributing

We welcome contributions to this project! Please refer to the CONTRIBUTING file for guidelines.

License

This project is licensed under the MIT License. See the LICENSE file for details.