Cloudflare worker is a serverless application based on v9 engine. The structure is a little bit different than the Node.js. Endpoints is familiar to most people in developing. But this seems to be different on worker. And it is useful especially when different services is running in the same worker. This one will show how to integrate traditional endpoint feature on cloudflare worker to likely have a fake endpoint handler. This is a sub-project of Financial website.
End-points:
Three end points is in this sample, they are arranged in three different purpose.
Check the AES Encrypt and Decrypt on Cloudflare worker to see the encryption and decryption methods using in
/authenticate
and/generate
, which is based onAES-GCM
/authenticate
return a password if the information, like token
and quizepartcode
matched with our record. Otherwise, return false;
/generate
Generate a user learning progress, will need quizpartcode in the body.
/sendmail
with a password as authentication to send email via sendgrid from [email protected]. This method is not enabled as sendgrid has been blocked by a lot of client.
Initial Handling
In each fetch request to worker, there will be a request with content pass to the worker, usually and officially returning method is
1 | addEventListener('fetch', event => { |
Reroute Request
Even though Worker is just JavaScript running on v8, we could just create a function to reroute each request based on the address user is requesting, like
1 | (endpoint, fn) => { |
In javascript, functions could wrapped using a variable, so, it is possible to create an object to contain everything, like
1 | const app = { |
Return a Response
Whenever needed in handleRequest
, it is as easy as calling app.post("/endPoint", functionName)
. And if null is returned, check next endpoint, otherwise the correct function will be called and executed. The reroute function like this:
1 |
|
notFoundHead
is the head content created as a js Object, like
1 | let notFoundHead = { |
Caution: Without a head containing the
Access-Control-Allow-Origin
, CORS error will be posted. Cloudflare worker seems to be strict on this thing. If any error or exception happened without anytry
…catch
, CORS will also be given. So, to be convenient in debugging, a good practice is always to return a Not found header when error happened in some cases.If method other than
GET
is used,Access-Control-Allow-Methods
should also be used. Otherwise, CORS error would also happen.And when posting fetch request, do not append header in this example because it is not appropriately handled and it might have problem in this case.
One function in the sample is handleAuthen
, and one another is handleGen
. Those two are normal functions and just return a new Response
at last would be fine. Like:
1 | /** |
Visiting Data part in request
Because the data section of the request is a Promise
object, it has to be called with await
and .json()
in order to visit it. Like: var data = await request.json();
.
SendMail through sendgrid
In the real case, sending a mail to the user is general. And sometimes, we have certain sensitive data that should not be promoted to client. This could be implemented through the following method in worker and called in client like this. That is what the third endpoint /sendmail
is doing. like:
1 | /** |
Caution:
sendgrid
will not return anything if the response status is ok, which could be checked witha.ok
in the previous sample. If wrong, a data will be returned and could be decoded to json Object
Run
In order to run and test locally, you need to install Cloudflare Worker Local client first.
Then do:
1 | wrangler dev |