On this page
File system APIs
Deno Deploy supports a limited set of the file system APIs available in Deno. These file system APIs can access static files from your deployments. Static files are for example:
- The files in your GitHub repository, if you deploy via the GitHub integration.
- The entrypoint file in a playground deployment.
The APIs that are available are:
- Deno.cwd
- Deno.readDir
- Deno.readFile
- Deno.readTextFile
- Deno.open
- Deno.stat
- Deno.lstat
- Deno.realPath
- Deno.readLink
Deno.cwd Jump to heading
Deno.cwd()
returns the current working directory of your deployment. It is
located at the root of your deployment's root directory. For example, if you
deployed via the GitHub integration, the current working directory is the root
of your GitHub repository.
Deno.readDir Jump to heading
Deno.readDir()
allows you to list the contents of a directory.
The function is fully compatible with Deno.
function Deno.readDir(path: string | URL): AsyncIterable<DirEntry>
The path can be a relative or absolute. It can also be a file:
URL.
Example Jump to heading
This example lists the contents of a directory and returns this list as a JSON object in the response body.
async function handler(_req) {
// List the posts in the `blog` directory located at the root
// of the repository.
const posts = [];
for await (const post of Deno.readDir(`./blog`)) {
posts.push(post);
}
// Return JSON.
return new Response(JSON.stringify(posts, null, 2), {
headers: {
"content-type": "application/json",
},
});
}
Deno.serve(handler);
Deno.readFile Jump to heading
Deno.readFile()
allows you to read a file fully into memory.
The function definition is similar to
Deno, but it doesn't support
ReadFileOptions
for
the time being. Support will be added in the future.
function Deno.readFile(path: string | URL): Promise<Uint8Array>
The path can be a relative or absolute. It can also be a file:
URL.
Example Jump to heading
This example reads the contents of a file into memory as a byte array, then returns it as the response body.
async function handler(_req) {
// Let's read the README.md file available at the root
// of the repository to explore the available methods.
// Relative paths are relative to the root of the repository
const readmeRelative = await Deno.readFile("./README.md");
// Absolute paths.
// The content of the repository is available under at Deno.cwd().
const readmeAbsolute = await Deno.readFile(`${Deno.cwd()}/README.md`);
// File URLs are also supported.
const readmeFileUrl = await Deno.readFile(
new URL(`file://${Deno.cwd()}/README.md`),
);
// Decode the Uint8Array as string.
const readme = new TextDecoder().decode(readmeRelative);
return new Response(readme);
}
Deno.serve(handler);
Note: to use this feature, you must link a GitHub repository to your project.
Deno Deploy supports the Deno.readFile
API to read static assets from the file
system. This is useful for serving static assets such as images, stylesheets,
and JavaScript files. This guide demonstrates how to use this feature.
Imagine the following file structure on a GitHub repository:
├── mod.ts
└── style.css
The contents of mod.ts
:
async function handleRequest(request: Request): Promise<Response> {
const { pathname } = new URL(request.url);
// This is how the server works:
// 1. A request comes in for a specific asset.
// 2. We read the asset from the file system.
// 3. We send the asset back to the client.
// Check if the request is for style.css.
if (pathname.startsWith("/style.css")) {
// Read the style.css file from the file system.
const file = await Deno.readFile("./style.css");
// Respond to the request with the style.css file.
return new Response(file, {
headers: {
"content-type": "text/css",
},
});
}
return new Response(
`<html>
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Example</h1>
</body>
</html>`,
{
headers: {
"content-type": "text/html; charset=utf-8",
},
},
);
}
Deno.serve(handleRequest);