All Projects → rajtatata → cloudflare-worker-rest-api

rajtatata / cloudflare-worker-rest-api

Licence: other
A cloudflare worker module which helps building REST Api quickly and easily, similar to express framework.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to cloudflare-worker-rest-api

Serverless Cloudflare Workers
Serverless provider plugin for Cloudflare Workers
Stars: ✭ 114 (+267.74%)
Mutual labels:  workers, cloudflare
Cfworker
A collection of packages optimized for Cloudflare Workers and service workers.
Stars: ✭ 152 (+390.32%)
Mutual labels:  workers, cloudflare
Create Google Shared Drive
Cloudflare Redesigned Script for creating a Shared/Team Drive
Stars: ✭ 93 (+200%)
Mutual labels:  workers, cloudflare
cloudflare-worker-graphql-ws-template
A template for WebSockets powered Cloudflare Worker project using graphql-ws
Stars: ✭ 21 (-32.26%)
Mutual labels:  workers, cloudflare
viteflare
Cloudflare workers meet Vite plugins
Stars: ✭ 35 (+12.9%)
Mutual labels:  workers, cloudflare
Google Drive Index
Index Google Drive Files Easily and Free
Stars: ✭ 205 (+561.29%)
Mutual labels:  workers, cloudflare
Worker Typescript Template
ʕ •́؈•̀) TypeScript template for Cloudflare Workers
Stars: ✭ 129 (+316.13%)
Mutual labels:  workers, cloudflare
vite-plugin-cloudflare
🔥Building Cloudflare workers is faster and easier using vite-plugin-cloudflare with node builtins like process and stream
Stars: ✭ 108 (+248.39%)
Mutual labels:  workers, cloudflare
cloudflare-worker-router
A super lightweight router (1.3K) with middleware support and ZERO dependencies for CloudFlare Workers.
Stars: ✭ 144 (+364.52%)
Mutual labels:  workers, cloudflare
worker-template-postgres
Reference demo and modified PostgreSQL driver to connect Cloudflare Workers to a relational database.
Stars: ✭ 75 (+141.94%)
Mutual labels:  workers, cloudflare
workers-jwt
Generate JWTs on Cloudflare Workers using the WebCrypto API
Stars: ✭ 67 (+116.13%)
Mutual labels:  workers, cloudflare
natural
Fastest Framework for NodeJS. Written in pure ES6+
Stars: ✭ 30 (-3.23%)
Mutual labels:  workers, cloudflare
flareon
🦊A cloudflare DNS over HTTPs resolver client library.
Stars: ✭ 14 (-54.84%)
Mutual labels:  cloudflare
express-mvc
A light-weight mvc pattern for express framework with minimum dependencies
Stars: ✭ 23 (-25.81%)
Mutual labels:  expressjs
node-uploadx
Node.js middleware for handling resumable uploads
Stars: ✭ 17 (-45.16%)
Mutual labels:  expressjs
Askme
Social media app to ask and answer user questions and interact with users
Stars: ✭ 16 (-48.39%)
Mutual labels:  expressjs
spid-express
Express middleware implementing SPID & Entra con CIE (Carta d'Identità Elettronica)
Stars: ✭ 27 (-12.9%)
Mutual labels:  expressjs
dummy-products-api
An api to fetch dummy e-commerce product 👕 👗 👖 👚 JSON data with placeholder images.
Stars: ✭ 102 (+229.03%)
Mutual labels:  expressjs
timeoff-server
TimeOff is an application that allows companies' employees to set vacations before they begin taking their time off. Implemented in modern tech stack i.e. Node, Express, MongoDB.
Stars: ✭ 33 (+6.45%)
Mutual labels:  expressjs
statusengine
New PHP based MySQL Backend for Naemon and Nagios 4 + responsive web frontend
Stars: ✭ 16 (-48.39%)
Mutual labels:  workers

Cloudflare Worker Rest Api

This library helps build serverless rest api with cloudflare workers

Description

The idea behind this package was to create a library that would be as close to express framework as possible, and that would make creating Rest Apis with Workers quick and easy.

Installing

Begin by installing the package with npm.

npm install cloudflare-worker-rest-api --save

Example App

For a fully working example check out this project

How to use

Import the package and initialize your app.

const restCfWorker = require("cloudflare-worker-rest-api");
const app = new restCfWorker();

// ....

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

In order for cloudflare to use your app, we need to call app.handleRequest on the fetch event listener.

Rest API

The supported methods are POST, GET, DELETE, PATCH, PUT and ANY.

// supports middlewares
app.use((req, res) => {
  // do some authenticate process
  req.isAuthenticated = true;
});

app.get("/path-here", (req, res) => {
  // access query
  const { filter, page } = req.query();

  return res.send({ status: 1, message: "Hello stranger!" });
});

// Three parameters for req.send method
// First one is Response Data
// Second one is Headers, by default it is set to {'content-type': 'application/json'}
// Third one is Status Code, by default it is set to 200
app.get("/path-here", async (req, res) => {
  // access header
  const contentType = await req.header("content-type");
  if (contentType === "application/json") {
    return res.send({ status: 1, message: "File Created!" }, 201);
  }

  return res.send(
    "This is a string response",
    { "content-type": "text/plain" },
    200
  );
});

app.get("/path-here", async (req, res) => {
  // access header
  const contentType = await req.header("content-type");
  if (contentType === "application/json") {
    return res.send({ status: 1, message: "This is a JSON response!" });
  }

  return res.send("This is a string response", {
    "content-type": "text/plain",
  });
});

app.post("/path-here", async (req, res) => {
  // access body
  const { username } = await req.body();

  if (!req.isAuthenticated) {
    // supports status code
    return res.send(
      // undefined to send default headers
      { status: 1, message: "Bro, you not supposed to be here!" },
      undefined,
      401
    );
  }
  return res.send({
    status: 1,
    message: "Hello stranger, why are you still here!",
  });
});

// supports path params
app.delete("/item/:itemId", (req, res) => {
  const { itemId } = req.params;

  return res.send({ status: 1, message: `Oh no, you deleted item ${itemId}` });
});

Routing

The package also supports routers, if you want to divide your code in multiple files.

// ./routers/auth.js
const restCfWorker = require("cloudflare-worker-rest-api");

const router = new restCfWorker();

router.post("/login", (req, res) => {
  return res.send({ status: 1, message: "Successfully logged in!" });
});

// export the router
module.exports = router;

Then you can call your router file in your index file

const restCfWorker = require("cloudflare-worker-rest-api");
const authRouter = require("./routers/auth.js");

const app = new restCfWorker();

// use router
app.use("/auth", authRouter);

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

The login route now would be POST /auth/login

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].