Skip to content
File

Blob: samples/nodejs-compat-fs/worker.js

javascript17 lines
1import { readFileSync, writeFileSync, existsSync } from 'node:fs';
2import { ok, strictEqual } from 'node:assert';
3 
4// File system operations at the top level scope are allowed
5const config = readFileSync('/bundle/config', 'utf8');
6strictEqual(config, 'config file');
7 
8export default {
9 async fetch(request) {
10 // All files in the /tmp directory are always per-request.
11 // When the request ends, the files are deleted.
12 ok(!existsSync('/tmp/hello.txt'));
13 writeFileSync('/tmp/hello.txt', 'Hello, World!', 'utf8');
14 return new Response(readFileSync('/tmp/hello.txt'));
15 }
16};