Basically, this is a boilerplate of:
- Basic
CRUDoperations (I have only coveredcreateandreadand) - API Docs generation via
apidoc Express.jslikeapp- Body parsing
- Central error handling/reporting
- Serving static content using
getAssetFromKV - Basic sanitization of
parametersfor APIs - ESLint and Prettier integrated
- Cloudflare Workers KV (a
NOSQLdatabase)
for Cloudflare Workers with familiarity of node.js.
Something like this: url
See Changelog
- A single file to register
APIs
allowedOriginswhich origins should be allowed
- Cleaned up.
- Its now much simple to follow.
- From 168 lines to 10 lines
- New API
/:slug/taken - It will check if the
slugchosen by user is already taken or not.
- Uses modularized
middlewarefunctions instead of from oneindex.jsfile
- Uses modularized
middlewarefunctions instead of from oneindex.jsfile
isSlugTakenreturn json with following keys:takenslug
ids used for generating unique shorted url slug put under its own helper file
- Returns all enabled
CORSheaders - some
snake_casevariable names changed tolowerCamelCasenames
isOriginAllowed~ Will check if therequest's origin is one of the allowed ones or not.handleWhenOriginNotAllowed~ Simply throwsAPIErrorinforming user that they are trying to request from non allowed origin.
- This will catch any
err(s) thrown by the APIs and return to the user the appropriate error or forwarding the error which was originally throw asAPIError.
- Simply attaches current
Dateobject to the request so that when sending a response, we can calculate the duration.
- It simply informs the user that the API they are looking for does not exist.
- It basically parses the
bodyof therequestappropriate to its content type.
- It simply just redirect the user to the
.docswhen visiting home page of the API i.e./root path.
- It takes care of
CORSas well as:- redirecting to
/docswhen visiting root path - serving docs from
KVwhen visiting/docs - also handling API requests
- central error catching when APIs throw errors
- redirecting to
- It serves our
docsi.e. all the generated HTML files fromKVdatabase and responds when cannot find or going haywire in the process.
Get to know what Cloudflare Workers really is.
In short, cloud functions without cold starts
You do need to put relevant details in wrangler.toml file.
If it is of any help, have a look at the wranglerYOUR_FILE.toml
From root directory:
wrangler devand it will pick up your changes on save like nodemon for node.js
This boilerplate uses apidoc to generate necessary docs for your micro service API. So get to know the basics.
In short, /src/templates/apidocs_template should not be touched. Same advice goes for /src/static_files.
cd /app && npm run build_docswould generate the docs
Cloudflare Workers, as of right now (16 Oct 2021), does not support
MongoDB connection. There is also, even if it was supported, it would take
more than 10ms of CPU time which is the limit for Cloudflare Workers.
/src/index.js~ this is your mainappand do whatever tweaks and changes needed.