Code Monkey home page Code Monkey logo

Comments (1)

hodgef avatar hodgef commented on August 20, 2024 1

Hey @subhendukundu,

Cloudflare Workers (therefore Apiker) will respond to any request to this route, regardless of the method. Apiker will also parse the data sent (if any) and make it available in your route's handler.

For example:

import { res, res_400 } from "apiker";

export const myRouteHandler = async ({
  request, // https://developers.cloudflare.com/workers/runtime-apis/request/
  body, // The body of your request, such as form params or plaintext, depending on request content-type
  headers, // Request headers. Response headers are located at `apiker.responseHeaders`
  matches, // Your query params and route parts
  state // The state method used to interact with permanent storage (check the docs for more info)
}) => {
  // If we want to allow POST only, we explicitly check for it :
  if(request.method !== "POST"){
     // returns 400 Bad Request error. You can also use `res("Invalid Method", 405)`
     // https://github.com/hodgef/apiker/blob/master/src/components/Response/Response.ts#L10
     return res_400();
  }

  // We'll return the request body passed, for example POST form parameters
  // `res` and `res_000` functions respond with JSON. To respond with raw text you can use `resRaw`
  // https://github.com/hodgef/apiker/blob/master/src/components/Response/Response.ts#L23
  return res({ body });
};
const routes = {
  "/users/myroute": myRouteHandler
};

Here's where the body parsing is done:
https://github.com/hodgef/apiker/blob/master/src/components/Request/Request.ts#L76

Here's an example of a handler for a POST route:
https://github.com/hodgef/apiker/blob/master/src/components/Auth/registerUser.ts#L9
image

As you can see, in the register handler's code I don't filter by method because it's not strictly necessary, all I look for is for body parameters.

Let me know if you have any questions regarding this.

Regards,
Francisco Hodge

from apiker.

Related Issues (2)

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.