Basically, this is a boilerplate of:
- Basic
CRUD
operations (I have only coveredcreate
andread
and) - API Docs generation via
apidoc
Express.js
likeapp
- Body parsing
- Central error handling/reporting
- Serving static content using
getAssetFromKV
- Basic sanitization of
parameters
for APIs - ESLint and Prettier integrated
- Cloudflare Workers KV (a
NOSQL
database)
for Cloudflare Workers with familiarity of node.js
.
Something like this: url
See Changelog
- A single file to register
API
s
allowedOrigins
which 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
slug
chosen by user is already taken or not.
- Uses modularized
middleware
functions instead of from oneindex.js
file
- Uses modularized
middleware
functions instead of from oneindex.js
file
isSlugTaken
return json with following keys:taken
slug
ids
used for generating unique shorted url slug put under its own helper file
- Returns all enabled
CORS
headers - some
snake_case
variable names changed tolowerCamelCase
names
isOriginAllowed
~ Will check if therequest
's origin is one of the allowed ones or not.handleWhenOriginNotAllowed
~ Simply throwsAPIError
informing 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
Date
object 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
body
of therequest
appropriate to its content type.
- It simply just redirect the user to the
.docs
when visiting home page of the API i.e./
root path.
- It takes care of
CORS
as well as:- redirecting to
/docs
when visiting root path - serving docs from
KV
when visiting/docs
- also handling API requests
- central error catching when APIs throw errors
- redirecting to
- It serves our
docs
i.e. all the generated HTML files fromKV
database 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 dev
and 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_docs
would 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 mainapp
and do whatever tweaks and changes needed.