hostlocal
v1.3.1
Published
Serve files from a directory over HTTP/1.1 and HTTP/2, with live reload notifications over a websocket. It will automatically create self-signed certificates.
Downloads
934
Maintainers
Readme
hostlocal
Serve files from a directory over HTTP/1.1 and HTTP/2, with live reload notifications over a websocket. It will automatically create self-signed certificates.
This server is quite opinionated about its defaults to make setup as easy as possible... if you have a problem shaped like mine.
NOTE:
This server is NOT intended for production use, or any other place where the clients are not fully-trusted. Simplifying assumptions have been made to optimize code size, performance, and usability that make its use in a production context inappropriate. Security issues that are filed that do not take this note into account will not be prioritized.
Run
# Serve the docs directory, and open index.html in your default browser.
npx hostlocal docs
CLI
Usage: hostlocal [options] [directory]
Arguments:
directory Directory to serve. (default: cwd)
Options:
-6, --ipv6 Listen on IPv6 only, if host supports both IPv4
and IPv6.
-c, --config <file> If the given file exists, import it as a module
and use its default export as the options. Name
is relative to cwd. Command line parameters
overwrite options from the config file. (default:
".hostlocal.js")
--certDir <directory> Directory, relative to cwd, to cache cert info.
(default: ".cert")
-e, --exec <shell command> Execute this command when the glob changes.
(default: "npm run build")
-g, --glob <pattern> Set of files to watch. If one of these changes,
execute the command in the --exec option. Can be
specified multiple times.
-h, --help display help for command
-H, --host <address> Hostname or IP address to listen on. "::" for
everything. (default: "localhost")
-i, --initial If glob is specified, run the exec command on
startup, before listening.
--logFile <file> If specified, JSON logs are written to this file.
--no-script If specified, JavScript will not be added to the
end of HTML files to do auto-refresh.
--notAfterDays <number> How many days is the certificate valid? (default:
7)
-o, --open <path> Open this path in the default browser. Relative
to server root and prefix, if specified. If
empty (""), do not open anything. (default: ".")
-p, --port <number> Port to serve content from. Use 0 to get an
unused port. (default: 8111)
-P, --prefix <string> Make all of the URLs served have paths that start
with this prefix, followed by a slash.
-q, --quiet Do less logging. Can be specified more than
once.
--rawMarkdown Do not process markdown into HTML.
-t, --timeout <number> Time, in ms, to allow exec to run.
-v, --verbose Do more logging. Can be specified more than
once.
-V, --version output the version number
Config files
Instead of using the command line, you can store your configuration in a file, by default called ".hostlocal.js". This is an ES6 module with a default export containing your config. Its default value is:
export default {
certDir: '.cert',
config: '.hostlocal.js',
exec: 'npm run build',
glob: [],
headers: {},
host: 'localhost',
index: ['index.html', 'index.htm', 'README.md'],
initial: false,
ipv6: false,
logLevel: 0, // -3: fatal, -2: error, -1: warn, 0: info, 1: debug, 2: trace
logFile: null,
notAfterDays: 7,
open: '.',
port: 8111,
prefix: '',
rawMarkdown: false,
script: true,
timeout: null,
};
API
Full API documentation is available.
Example:
import {hostLocal} from 'hostlocal';
const server = await hostLocal({port: 8111});
server.on('listen', baseURL => console.log(baseURL.toString()));
await server.start();