Coder Social home page Coder Social logo

cloudworker-router's People

Contributors

danbars avatar dependabot[bot] avatar himyu avatar kidylee avatar lambirou avatar markusahlstrand avatar pbshgthm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cloudworker-router's Issues

Middleware auth

I'm not sure how to implement middleware. If an auth middleware passes, how do I continue onto the next handler?

How to render a external html file?

Hello,

I want to know how to send file to response/render an external html file like Express.js response.sendFile(__dirname + './home.html');

Can you help me?

Unable to use middleware

I'm trying to add authentication middleware to a route, the same way I use it on Express.

    router.get('/e', async (ctx) => {
        ctx.body = 'Hello World2';
        ctx.status = 200;
    }); 

    router.get('/a', userAuthCheck, async (ctx) => {
        ctx.body = 'Hello World2';
        ctx.status = 200;
    });

The userAuthCheck has code that validates a user's credentials and gets their user ID and some other information which I use later in the code. When I include the middleware, I get 404 however without it everything is fine.

Even if I get 404, I can console log in the middleware and see it so clearly the code is being reached.

I was wondering whether I'm doing it wrong or if it's just not supported this way?

Thanks again!

Cors

How to pass allow request from origin?

No Request or Query data in the context object

Hello!

Just checked out this module and wanted to try it.

After setting it up, it seems like there is no data inside ctx.request and ctx.query etc. event though a form body was posted.
Same happens with simple GET requests.
Is this a known issue or just an user error?

This is my setup:

index.ts

import { Router } from 'cloudworker-router'

const router = new Router();

router.post('/hello/:name', async (ctx) => {
    return new Response(JSON.stringify(ctx));
  });

export default {
    async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
        return router.handle(request, env, ctx);
    },
};

And this is the result:

image

TypeScript support

Hello,

Do you plan to add TypeScript support for this module ?

Thanks.

Pass bindings to routes context

With the new module worker syntax (https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker) bindings are no longer global. Instead they are passed as a second argument to exportefetch function. With that being said, it would be useful to be able to pass those bindings to the routes' context, either within state object or within it's own special property env / bindings.

A work around is you can pass a modified request with the bindings to the Router.handle, but this causes a typing issue & then to use the bindings you need to dig into ctx.event.request. It would be preferable to be able to add the bindings directly to routes' context.

Redirects?

Hi,

Is there a way to handle redirects/301/302?

Thanks,
Chris

Middlewares not working right

I was just trying to use your router to migrate an API to cloudflare workers, while testing I noticed that the middleware system doesn't work right.

This will return 404 for no reason:

const Router = require('cloudworker-router')
const router = new Router()

router.get('/test', async (ctx, next) => {
    next()
}, async ctx => {
    ctx.status = 204
})

addEventListener('fetch', event => {
    event.respondWith(router.resolve(event))
})

Maybe I'm just doing it wrong, but it's not really documented.

Enabling CORS

Hello, how can I add "Access-Control-Allow-Origin" header in my requests?
I tried few things but nothing works till now, I assume that there is a easier method to do it?
My code looks like this:

import { Router } from "cloudworker-router";
import cors from "cors";
const router = new Router();

router.get("/search", async (context) => {
  const symbol = new URL(context.request.url).searchParams.get("query");
  return fetch(`https://query1.finance.yahoo.com/v11/finance/quoteSummary/${symbol}?modules=financialData`);
});

router.get("/getSymbolData", async (context) => {
  const query = new URL(context.request.url).searchParams.get("symbol");
  return fetch(`https://query1.finance.yahoo.com/v1/finance/search?q=${query}`);
});

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    return router.handle(request, env, ctx);
  },
};

No License In Repo

This package is listed on NPM as MIT licensed, but there is no license in the REPO which makes forking it (or even using it) risky. Can you consider adding an explicit license in your repo? ie: LICENSE.md file

Cannot run from `wrangler dev`

Hey there, I followed the example code to set up a server. For reference, here is my code:

const Router = require("cloudworker-router");

const app = new Router();

console.log(Router);

app.get("/pet", async (ctx) => {
    var votes = await ssfinal.get("votes");
    var names = JSON.parse(await ssfinal.get("names"));
    var name = ctx.params.name;
    await ssfinal.put("votes", votes+1);
    votes += 1;
    names.push(name);
    await ssfinal.put("names", JSON.stringify(names));
    return new Response(votes);
});

app.get("/votes", async (ctx) => {
    var votes = await ssfinal.get("votes");
    return new Response(votes);
})

addEventListener("fetch", (event) => {
    event.respondWith(app.resolve(event));
});

But when I run wrangler dev, I get this error: ```
▲ [WARNING] Converting "require" to "esm" is currently not supported

index.js:1:15:
  1 │ const Router = require('cloudworker-router');
Uncaught TypeError: Router is not a constructor

I assume this is an issue with not packing everything into one file with webpack. Any idea how I can do this?

Error previewing from "wrangler preview"

First of all, thanks for creating this. I've been looking in using Cloudflare Workers more and this will really help make it easier.

When I install the module from NPM and make an index.js then run wrangler preview, I see this. https://i.imgur.com/32OKd0I.png

const router = require('cloudworker-router');

router.get('/', async (ctx) => {
    ctx.body = 'Hello World';
    ctx.status = 200;
});

addEventListener('fetch', event => {
    event.respondWith(router.resolve(event));
})

Is there any other code I need which I can find an example from? I'm terribly sorry if this is a dumb question.

Thanks!

Manual return instead of setting ctx.status and ctx.body only

Nice repo, thanks!

However I might have missed something down the road: what can we do besides set ctx.status and ctx.body ??

I naively tried:

router.post('/hello', async (ctx) => {
  try {
    const body = await ctx.event.request.json()
    ctx.body = JSON.stringify(body)+'\n';
    ctx.status = 200;      
  } catch (error) {
    ctx.status = 401;
  }
});

status code is never set because of the await function.

Is there a way to decide when to return?

Something like:

router.post('/hello', async (ctx) => {
  try {
    const body = await ctx.event.request.json()

    return res.status(200).json(body)
  } catch (error) {
    return res.status(401)
  }
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.