nx.js
Functions

createStaticFileHandler

createStaticFileHandler(root, opts?): (req) => Promise<null | Response>

Creates an HTTP handler function which serves file contents from the filesystem.

Parameters

ParameterTypeDescription
rootPathLikeRoot directory where static files are served from
opts?StaticFileHandlerOptionsOptional options object

Returns

Function

Parameters

ParameterType
reqRequest

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;
  }
});

On this page