Functions
createStaticFileHandler
createStaticFileHandler(root, opts?): (req) => Promise<null | Response>
Creates an HTTP handler function which serves file contents from the filesystem.
Parameters
| Parameter | Type | Description |
|---|---|---|
root | PathLike | Root directory where static files are served from |
opts? | StaticFileHandlerOptions | Optional options object |
Returns
Function
Parameters
| Parameter | Type |
|---|---|
req | Request |
Returns
Promise<null | Response>
Example
import { createStaticFileHandler, listen } from '@nx.js/http';
const fileHandler = createStaticFileHandler('sdmc:/switch/');
listen({
port: 8080,
async fetch(req) {
let res = await fileHandler(req);
if (!res) {
res = new Response('File not found', { status: 404 });
}
return res;
}
});