Giter VIP home page Giter VIP logo

cfw-ts-apiserver-template's Introduction

CloudFlare Workers Typescript REST API Boilerplate

Author: Patricio Tena

This is a REST API boilerplate for CloudFlare Workers using Typescript. This template uses subrouting to give a better organization for each hanlder and its respective functions.

Parent Router

First you create a parent router as follows:

const parent = Router()

parent
  .all('/v1/handler*', handlerRoutes.handle)
  .all('*', () => missing('Root API could not find that endpoint.'))

As you can see in the example above, the parent router has a handler for each hanlder subrouter. I personally create each subrouter routes in the /routes folder, then export it to the handler.ts.

Child Routers

Each child router must have a base defined:

const v1 = Router({ base: '/v1' }) 

After that you can use it as a regular router:

import {handleFetch} from '../handlers/handler'
v1
  .get('/handler/test', () => new Response("Testing!", { status: 200 }))
  .get('/handler',handleFetch)
  .all('*', () => missing('API v1/handler could not find that endpoint.'))

export default v1

The handleFecth function is imported from the ./handlers folder.

Handlers

A basic structure for a function hanlder can look something like this:

export async function handleFetch(event: Event){
    try{
        return new Response(JSON.stringify({
            status: 200,
            message: 'Ok',}), {
        status: 200,
        headers: {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        }
        })
    }catch(err){
        console.log(err);
    }
}

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.