{"id":17093927,"url":"https://github.com/rajtatata/cloudflare-worker-rest-api","last_synced_at":"2025-04-12T22:54:54.708Z","repository":{"id":52424461,"uuid":"235348160","full_name":"rajtatata/cloudflare-worker-rest-api","owner":"rajtatata","description":"A cloudflare worker module which helps building REST Api quickly and easily, similar to express framework.","archived":false,"fork":false,"pushed_at":"2021-04-29T18:22:02.000Z","size":5,"stargazers_count":35,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T22:54:50.540Z","etag":null,"topics":["cloudflare","express","expressjs","workers"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rajtatata.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-21T13:23:58.000Z","updated_at":"2025-01-21T14:26:11.000Z","dependencies_parsed_at":"2022-09-16T15:10:40.878Z","dependency_job_id":null,"html_url":"https://github.com/rajtatata/cloudflare-worker-rest-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajtatata%2Fcloudflare-worker-rest-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajtatata%2Fcloudflare-worker-rest-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajtatata%2Fcloudflare-worker-rest-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajtatata%2Fcloudflare-worker-rest-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rajtatata","download_url":"https://codeload.github.com/rajtatata/cloudflare-worker-rest-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248643048,"owners_count":21138353,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare","express","expressjs","workers"],"created_at":"2024-10-14T14:09:47.516Z","updated_at":"2025-04-12T22:54:54.688Z","avatar_url":"https://github.com/rajtatata.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cloudflare Worker Rest Api\n\nThis library helps build serverless rest api with cloudflare workers\n\n## Description\n\nThe 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.\n\n### Installing\n\nBegin by installing the package with npm.\n\n```\nnpm install cloudflare-worker-rest-api --save\n```\n\n### Example App\n\nFor a fully working example check out this [project](https://github.com/rajtatata/cloudflare-worker-example-rest-api)\n\n## How to use\n\nImport the package and initialize your app.\n\n```js\nconst restCfWorker = require(\"cloudflare-worker-rest-api\");\nconst app = new restCfWorker();\n\n// ....\n\naddEventListener(\"fetch\", (event) =\u003e {\n  event.respondWith(app.handleRequest(event.request));\n});\n```\n\nIn order for cloudflare to use your app, we need to call `app.handleRequest` on the fetch event listener.\n\n### Rest API\n\nThe supported methods are POST, GET, DELETE, PATCH, PUT and ANY.\n\n```js\n// supports middlewares\napp.use((req, res) =\u003e {\n  // do some authenticate process\n  req.isAuthenticated = true;\n});\n\napp.get(\"/path-here\", (req, res) =\u003e {\n  // access query\n  const { filter, page } = req.query();\n\n  return res.send({ status: 1, message: \"Hello stranger!\" });\n});\n\n// Three parameters for req.send method\n// First one is Response Data\n// Second one is Headers, by default it is set to {'content-type': 'application/json'}\n// Third one is Status Code, by default it is set to 200\napp.get(\"/path-here\", async (req, res) =\u003e {\n  // access header\n  const contentType = await req.header(\"content-type\");\n  if (contentType === \"application/json\") {\n    return res.send({ status: 1, message: \"File Created!\" }, 201);\n  }\n\n  return res.send(\n    \"This is a string response\",\n    { \"content-type\": \"text/plain\" },\n    200\n  );\n});\n\napp.get(\"/path-here\", async (req, res) =\u003e {\n  // access header\n  const contentType = await req.header(\"content-type\");\n  if (contentType === \"application/json\") {\n    return res.send({ status: 1, message: \"This is a JSON response!\" });\n  }\n\n  return res.send(\"This is a string response\", {\n    \"content-type\": \"text/plain\",\n  });\n});\n\napp.post(\"/path-here\", async (req, res) =\u003e {\n  // access body\n  const { username } = await req.body();\n\n  if (!req.isAuthenticated) {\n    // supports status code\n    return res.send(\n      // undefined to send default headers\n      { status: 1, message: \"Bro, you not supposed to be here!\" },\n      undefined,\n      401\n    );\n  }\n  return res.send({\n    status: 1,\n    message: \"Hello stranger, why are you still here!\",\n  });\n});\n\n// supports path params\napp.delete(\"/item/:itemId\", (req, res) =\u003e {\n  const { itemId } = req.params;\n\n  return res.send({ status: 1, message: `Oh no, you deleted item ${itemId}` });\n});\n```\n\n### Routing\n\nThe package also supports routers, if you want to divide your code in multiple files.\n\n```js\n// ./routers/auth.js\nconst restCfWorker = require(\"cloudflare-worker-rest-api\");\n\nconst router = new restCfWorker();\n\nrouter.post(\"/login\", (req, res) =\u003e {\n  return res.send({ status: 1, message: \"Successfully logged in!\" });\n});\n\n// export the router\nmodule.exports = router;\n```\n\nThen you can call your router file in your index file\n\n```js\nconst restCfWorker = require(\"cloudflare-worker-rest-api\");\nconst authRouter = require(\"./routers/auth.js\");\n\nconst app = new restCfWorker();\n\n// use router\napp.use(\"/auth\", authRouter);\n\naddEventListener(\"fetch\", (event) =\u003e {\n  event.respondWith(app.handleRequest(event.request));\n});\n```\n\nThe login route now would be `POST /auth/login`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frajtatata%2Fcloudflare-worker-rest-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frajtatata%2Fcloudflare-worker-rest-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frajtatata%2Fcloudflare-worker-rest-api/lists"}