3.2.0 • Published 10 months ago

fs-serve v3.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Serve static files with HTTP

Serve static files with:

  • configurable directory listing
  • a wide range of pre-configured mime-types
  • charset utf-8 for text file types
  • configurable caching (including etags and custom max-age)
  • streamed responses
  • server side include (SSI)

Usage (CLI)

Serve the content of this directory. Directory listing are enabled:

npm install -g fs-serve
fs-serve

Serve the content of the wwww on port 3333 to localhost:

fs-serve path www port 3333

Serve the content of the wwww on port 3333 on all networks, enable SSI virtual and file includes for .shtml files and open your default browser:

fs-serve path www port 3333 host 0.0.0.0 ssi.shtml virtual,file open

SSI will enable processing of directives similar to those supported by Apache mod_include (https://httpd.apache.org/docs/2.4/mod/mod_include.html)

  • virtual include: <!--#include virtual="url/relative/to/current/request">, <!--#include virtual="/absolute/url/path">
  • file include: <!--#include file="path/relative/to/current/file">, <!--#include file="/absolute/path">
  • url include: <!--#include url="http://host/path">, <!--#include url="/absolute/url/path">

SSI processing should not be combined with user generated content to avoid security issues. URL include requires extra caution. Included files may be outside the currently served directory.

Usage (JS)

npm install fs-serve

Note that only ES module imports are supported (not require)

import Server from "fs-serve";

let httpServer = await new Server("www").listen(3000);
console.log(await (await fetch("http://localhost:3000/")).text());
httpServer.close();

API

import Server from "fs-serve";

let directory = "www";
// Create a new server
const server = new Server(directory, {
  directoryList: false,
  log: (url, statusCode, result) => console.log(url, statusCode, result),
  maxAge: 2,
  ssi: {
    extension: ".shtml",
    handlers: [Server.includeVirtual]
  }
});
  • directoryList - set to true to enable directory lists - default false
  • log - provide a callback for custom logging, or set to falsy to disable logging - default console.log
  • maxAge - how many seconds a client should cache a response - default 2 seconds
  • ssi - a list of extensions with SSI handlers

Serve a request in an existing web server:

import Server from "fs-serve";
import http from "http";

const server = new Server("www");

http.createServer((req, res) => server.serve(request, response));

Create an http server and listen on port and host:

import Server from "fs-serve";

let port = 4444;
let host = "localhost";
let httpServer = await new Server("www").listen(port, host);
httpServer.close();

The port will be selected automatically if omitted, and the server will listen on all addresses if the host is omitted. See https://nodejs.org/api/net.html#serverlisten for details.

3.2.0

10 months ago

3.1.0

1 year ago

3.0.0

1 year ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.0

3 years ago