Let your computer's clock run TaskKit's backup every night with cron or Task Scheduler, and write a serverless function that answers a phone with today's tasks, checked against Cloudflare's types before every deploy.
TypeScript
Node.js
Automation and serverless functions. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
In the last lesson TaskKit got a backup script, and it works every
time you run it. Last night you forgot to run it. Your friend has a
wish of their own, too: a link on their phone that shows today's
tasks, without your laptop left on all night for lesson A1.3's server. Both
need a program that nobody starts by hand.
That is the difference from the last lesson: something else presses Enter. In
this lesson your computer's clock starts the backup every night, and you can
see afterwards how it went, and a platform on the internet runs a small
TaskKit function each time the phone asks, checked against the platform's own
types before you upload it.
Programs that something else starts
Lesson A1.3's server starts once and then runs all day, waiting for requests.
The last lesson's taskkit runs only when you type it. The two in
this lesson are like that command, except that something else types
it: your computer's clock, at midnight every night, or a platform on
the internet, when your friend's phone sends a request. Here is one
day of TaskKit:
Server
You
Cloud
Clock
Server, from 0 to 6: runs all day, waitingrunsallday,waitingrunsallday,waiting
Cloud, at 2: GET /todayGET/todayGET/today
You, at 3: taskkittaskkittaskkit
Cloud, at 5: GET /todayGET/todayGET/today
Time
00:00
04:00
08:00
12:00
16:00
20:00
24:00
Lesson A1.3's server runs all day; your command, a serverless
function and a scheduled script run only when someone or something
starts them
Nobody watches these programs run. What you learn afterwards is
what they leave behind: an exit code or a log for the clock's job, a
response for the platform's.
Automation: something else presses Enter
Automation means a program starts your scripts instead of you.
The one every computer has is a scheduler, which starts a command at the
times you give it, like the alarm in this course's first lesson. macOS and
Linux have cron, Windows has Task Scheduler. Task Scheduler keeps the
last exit code of the script (the number from the last lesson that says
whether it worked); cron keeps no code, only what the job prints, so on macOS
and Linux the job writes a log.
A scheduler starts a job only while the computer is on and awake: a
laptop asleep through the night skips that night's backup, and cron
does not run it later. Before you give it the job, ask your terminal where
Node is. which on macOS and Linux, and Get-Command in PowerShell,
print the file the PATH finds for a command:
Yours may be another path.
On macOS and Linux the job is one line for cron: five fields for the
time, then the command. 0 0 * * * is minute 0 of hour 0, midnight,
on every day of every month, whatever the weekday; 15 18 * * *
would be 18:15 every evening. >> backup.log adds what the script
prints to the end of backup.log, since nobody sees a screen. In a
command, 1 is a program's ordinary output and 2 its standard
error, so 2>&1 sends standard error where the ordinary output
goes, into the log too. Then give the scheduler the job:
The && in the cron line earns its place: if the project folder were
ever moved, cd would fail with a non-zero exit code, and the backup
would not run in the wrong folder.
Schedulers are not the only automation you will meet. A check that
runs on GitHub each time someone proposes a change to the code,
CI (continuous integration), is automation too, and
it reads your scripts' exit codes exactly as && does. It comes in
CI: running your checks on every pull request, later in this
module.
Serverless functions: run only when called
Your friend's phone needs an address to ask, /today, and something to answer
it. Lesson A1.3's handler can answer, but a server has to run on a computer
that is switched on all night. A serverless function is a handler you give
to a platform, a service that runs other people's code on its own
computers. Cloudflare Workers, Vercel Functions, Netlify Functions, AWS
(Amazon Web Services) Lambda and Deno Deploy are some you will hear of. There
are still servers; they are just not yours. Press Next step to follow one
request, or let it play:
Phone
Platform
Yourfunction
1
Phone to Platform: GET/today
2
Platform to Your function:
All 6 steps shown.Step 1 of 6: Phone → Platform — GET /todayStep 2 of 6: Platform → Your function — start a copyStep 3 of 6: Platform → Your function — fetch(request)Step 4 of 6: Your function → Platform — ResponseStep 5 of 6: Platform → Phone — 200, today's tasksStep 6 of 6: Platform — stop the copy
All 6 steps shown.
That is the longest path. Often the platform keeps the copy from the
last request for a while and skips the steps start a copy and
stop the copy; you never decide which. Platforms charge for the
requests and the computing time they use, after a free allowance, so
a small function like this one often costs nothing (each platform's
pricing page, checked on 26 November 2026; plans change, so read it
before you rely on it).
On Cloudflare Workers and Deno Deploy, and on Vercel when you write
export default { fetch }, the
function is the shape lesson A1.3 wrote: from a Request to a Response. The
file hands it over with export default, which marks the one thing a file
hands over when another file imports it; the platform imports your file and
calls its fetch. This block is that file, with two lines under it that call
fetch as the platform would; the real file has only the lines above them.
Press Run:
http://localhost:8787 is where Cloudflare's tool, Wrangler,
serves your function on your own computer while you work on it.
npm create (npm is the Node package manager) downloads and runs a
tool that makes a new project; this one makes a Workers project,
asking a few questions first. In the taskkit folder, run:
For this lesson's project, give these answers (the questions as the
tool asked them on 26 November 2026; they change from version to
version):
In which directory do you want to create your application?
taskkit-today
What would you like to start with? Hello World example
Which template would you like to use? Worker only
Which language do you want to use? TypeScript
Do you want to add an AGENTS.md file to help AI coding tools
understand Cloudflare APIs? No (AI is artificial
intelligence; APIs, application programming interfaces, are what
a service lets programs ask of it)
Do you want to use git for version control? No
Do you want to deploy your application? No
Then go into the new project:
The template also made a test folder, with tests for Cloudflare's
own test runner. TaskKit's npm test, run in taskkit, would find
them and fail on them, so delete that folder: in VS Code's Explorer,
right-click taskkit-today/test, choose Delete, and confirm with
Move to Trash (Move to Recycle Bin on Windows).
In taskkit-today, npm run dev starts Wrangler at that address.
npm run deploy opens a browser to sign you in to Cloudflare, where
a free account is enough. Cloudflare's pricing page on 26 November
2026 gave the free plan 100,000 requests a day, and plans change.
The first time, it also asks for a name
for your workers.dev address. Then it uploads the function, which
is called deploying it, and prints
its address on the internet, ending in workers.dev. That address,
with /today at the end, is the link your friend opens. The tasks in
the real function would come from a store the platform offers, a
place it keeps data between requests, not from a list written into
the code.
Where TypeScript helps is the shape. The platform will call fetch
with a Request and expect a Response back, and the project comes
with the platform's types to say so. Its src/index.ts starts with
a comment of twelve lines and a blank one, and under it are these
five, lines 14 to 18 of the file:
async fetch(…) { … } written inside an object is a method, the way lesson
13.1 wrote them in a class. satisfies, Level 2's The satisfies
operator, checks a value against a type without changing the value's own
type, and gives the parameters their types from it: request is a Request
with no annotation. env holds the settings and stores you give the function,
typed by Env, a type the project writes for them, and ctx lets it finish
work after answering. What the check is worth depends on running it: the
common mistake below is what happens when nobody does.
Predict before you run
Predict
What does the second line print, on this page?
So what a function must keep, such as a task your friend adds, goes
in a store the platform offers or in a database, never in a variable
or a file of its own.
The common mistake: deploying unchecked
npm run deploy uploads your function as it is. It never runs the
check, so the platform's types protect you only if you ask them.
Common mistake
A misspelled fetch, deployed unchecked
One letter is wrong in src/index.ts:
That is line 15 of the file, the second of the five above.
npm run deploy uploads it without a
word, and the platform finds no fetch to call: every request fails
with status 500, and your friend's phone gets an error instead of
the list. npm run dev shows the same failure before you deploy:
open http://localhost:8787/today, and the page and Wrangler's log
both say the error above.
Error: Handler does not export a fetch() function.
The fix
Make the deploy run the check first. In the project's package.json,
put tsc and && in front of wrangler deploy:
tsc here is the TypeScript the project installed, 5.9 on 26
November 2026 (the template takes any 5.x, so yours may be newer),
and TypeScript 7 words its errors the same way. With the typo, the check
ends with a non-zero exit code and && never starts the upload:
The error's first line is one line in your terminal, broken here to
fit. Three TS7006 errors follow, Parameter 'request' implicitly has an 'any' type. and the same for env and ctx: with no
fetch, satisfies has nothing to give them types from. Rename
fetsh to fetch, and all four are gone.
A deploy is a script like any other, and && stops it the way it
stopped the last lesson's report. Whenever a command uploads or
publishes something, put the check in front of it.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
The first one runs on this page: Check runs your code, then its
checks, and shows what TypeScript or a test says.
Recap
Recap · 3 questions
Check yourself
Why does taskkit.cron start with a PATH= line?
Check yourself
What makes a function serverless?
Check yourself
What does satisfies ExportedHandler check?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
Automation and serverless functions
What things are
Word
What it means
Automation
Programs starting your scripts instead of you
Scheduler
Starts commands at set times: cron, Task Scheduler
Serverless function
A handler a platform runs when requests arrive
Platform
A service that runs your code: Workers, Vercel
Wrangler
Cloudflare's tool: localhost:8787 while you work
Deploy
Upload your function to the platform
Code and commands
Write
To
which node
Where node is; (Get-Command node).Source on Windows
PATH=…
Give cron the folders to search, Node's first
0 0 * * *
cron's time: minute, hour, day, month, weekday
>> backup.log 2>&1
Add all the output to a log
crontab -l
List your cron jobs
crontab taskkit.cron
Replace them with the file's
Register-ScheduledTask
Give Task Scheduler a job, on Windows
Start-ScheduledTask
Run the task now
LastTaskResult
The task's last exit code
Unregister-ScheduledTask
Remove the task
npm create cloudflare@latest
Make a Workers project
npm run dev
Serve it at localhost:8787 while you work
npm run deploy
Where to go next
You can now let your computer's clock start a script every night and
see whether it worked, and write a serverless function that answers a
request, checked against its platform's types before it is deployed.
That is the last place TypeScript runs in this part: in a tab, on a
server, on a phone and a desktop, and with no screen at all.
Next is Git and GitHub: saving and sharing your work, the first of
the tools you will meet around your code. It keeps taskkit.cron,
the scripts and the Worker in one history you can go back through,
and puts them on GitHub, where the rest of this module builds on
them.
Before you move on, press Edit on the src/index.ts block you ran
and add a /tomorrow path that answers with the tasks due on
2026-10-31. Predict what /tomorrow and /next-week each answer,
then call both under the /today line and run it. Read each answer
with .text(): the 404's body is plain text, not JSON.
Clock, at 6: backup
backupbackup
startacopy
3
Platform to Your function: fetch(request)
4
Your function to Platform: Response
5
Platform to Phone: 200,today'stasks
6
Platform, to itself: stopthecopy
The platform starts your function for one request and
stops it after the answer
Upload it; it prints a workers.dev address
export default worker
Hand the platform your file's handler
satisfies ExportedHandler
Check the handler against Cloudflare's type
"tsc && wrangler deploy"
Check before every upload
How it runs
Rule
What it means
cron lacks your PATH
Give it a PATH= line
Task Scheduler's folder
Name it, or it starts in System32
Asleep through the night
The job is skipped
A copy may be new
Nothing in memory lasts between requests
Deploy checks nothing
Put tsc && in front of it
1
Write the schedule
A scheduler runs your command with no terminal. cron also starts it with
almost none of your settings: not your working directory, and not your PATH. A
line with only npm run backup in it backs up nothing, because cron cannot
find npm (the Node package manager); the log gets
/bin/sh: npm: command not found and nothing else.
That is lesson 1.2's PATH again, so the file below gives cron one: the folder
which node printed, without /node at the end, then two system folders.
Make a file taskkit.cron in taskkit with these two lines. The
second is the job:
taskkit.crontext
PATH=/usr/local/bin:/usr/bin:/bin0 0 * * * cd ~/projects/taskkit && npm run backup >> backup.log 2>&1
Then hand the file to cron:
bash
crontab -lcrontab taskkit.cron
crontab -l lists the jobs you already have, or says you have none.
crontab taskkit.cron replaces all of them with the file's, so if
the first command listed any, copy them into taskkit.cron first.
macOS may ask whether Terminal may administer your computer: choose
OK.
2
See whether it worked
After midnight, backup.log in taskkit ends with npm's two lines
and the backup's own. tail -n 4 prints a file's last four lines:
The date is the night's. To try it sooner, put a time a few
minutes ahead in the line and run crontab taskkit.cron again. To
stop the job, delete its line, and hand the file to cron the same
way.
1
Write the schedule
Task Scheduler runs your command as you, with your PATH, while you are signed
in, but with C:\Windows\System32 as its working directory unless you name
another. It starts Node itself rather than a command line, so the task names
node and the script, where cron's line runs npm run backup (npm is the
Node package manager). By default it also skips the task on a laptop running
on battery, so the settings below allow that. The task says which folder, as
well as what to run, when, and under what name. Paste the whole block and
press Enter. A name that starts with $ keeps a value for the next
lines, $HOME is your home folder, and a backtick at the end of a line
continues the command on the next one.
The last command prints a small table with the task, Ready.
2
See whether it worked
Task Scheduler keeps the last exit code itself. Start the task now,
give it a few seconds to finish, then ask for it:
0 means the backup worked. The Task Scheduler app shows the
same number as Last Run Result. To stop the job,
Unregister-ScheduledTask -TaskName "TaskKit backup" removes it,
after asking you to confirm.
1
Write the schedule
A scheduler runs your command with no terminal. cron also starts it with
almost none of your settings: not your working directory, and not your PATH. A
line with only npm run backup in it backs up nothing, because cron cannot
find npm (the Node package manager); the log gets
/bin/sh: 1: npm: not found and nothing else. That
is lesson 1.2's PATH again, so the file below gives cron one: the folder
which node printed, without /node at the end, then two system folders.
Make a file taskkit.cron in taskkit with these two lines. The
second is the job:
Then hand the file to cron:
crontab -l lists the jobs you already have, or says no crontab for ana.
crontab taskkit.cron replaces all of them with the file's, so if
the first command listed any, copy them into taskkit.cron first.
2
See whether it worked
After midnight, backup.log in taskkit ends with npm's two lines
and the backup's own. tail -n 4 prints a file's last four lines:
The date is the night's. To try it sooner, put a time a few
minutes ahead in the line and run crontab taskkit.cron again. To
stop the job, delete its line, and hand the file to cron the same
way.
Exercise · Read the code
Read a cron time
A line in taskkit.cron starts with 30 7 * * 1. When does cron run
its command?
Exercise · Read the code
A log without the error
A taskkit.cron has the PATH= line, but its job ends in
>> backup.log, with no 2>&1. One night tasks.json is missing.
What does backup.log show for that night?
Exercise · Read the code
A night with the lid closed
The backup runs at 0 0 * * *. Your laptop sleeps from 23:00, with
its lid closed, and you open it at 8:00. When is that night's backup
made?
Output (its start)text
> taskkit-today@0.0.0 deploy> tsc && wrangler deploysrc/index.ts:15:8 - error TS2561: Object literal may only specify knownproperties, but 'fetsh' does not exist in type 'ExportedHandler<Env,unknown, unknown, unknown>'. Did you mean to write 'fetch'?15 async fetsh(request, env, ctx): Promise<Response> { ~~~~~
bash
npm create cloudflare@latest
bash
cd taskkit-today
bash
npm run deploy
Outputtext
/usr/local/bin/node
C:\Program Files\nodejs\node.exe
/home/ana/.nvm/versions/node/v24.11.1/bin/node
src/index.ts, as the page runs itts
interface Task { title: string; due: string;}const tasks: Task[] = [ { title: "Pay rent", due: "2026-10-30" }, { title: "Call Mom", due: "2026-11-02" },];const today = "2026-10-30"; // the real function asks the clockasync function handle(request: Request): Promise<Response> { const url = new URL(request.url); if (url.pathname === "/today") { return Response.json(tasks.filter((task) => task.due === today)); } return new Response("No such path", { status: 404 });}const worker = { fetch: handle };export default worker;// What the platform does when GET /today arrivesconst answer = await worker.fetch(new Request("http://localhost:8787/today"));console.log(answer.status, await answer.json());
Handler stands in for the platform's own type, and satisfies
gives request its type from it. The check refuses this function in
two places, and the last line fails too until both are fixed: a
Request has no pathname of its own, and the 404 is a string where
the platform needs a Response. Fix both, so /today
answers with today's tasks and any other path with status 404.
Loading the editor…
bash
which node
(Get-Command node).Source
which node
taskkit.crontext
PATH=/home/ana/.nvm/versions/node/v24.11.1/bin:/usr/bin:/bin0 0 * * * cd ~/projects/taskkit && npm run backup >> backup.log 2>&1