serves
A tiny HTTP server CLI and API, mainly for local development.
Serves a default index.html
and an optional JavaScript entry-point. By default, uses localhost
and port 8080
(or searches for the next available port).
When using the programmatic API, the entry
and index
can be a middleware(req, res)
function. For example, to bundle with browserify.
Install
npm install serves [-g|--save]
CLI Example
The following creates a server that hosts a default index.html
with a <script>
tag pointing to src/index.js
. All other static content (images, etc) is served from the public
root directory.
serves src/index.js --root public --port 9000
API Example
A simple example:
var serves = var path =
Or see example/index.js, which mimics wzrd and bundles CommonJS on request.
Usage
CLI
Usage:
serves [entry.js] [opts]
Options:
--title, -t HTML title
--root, -r root directory for static files (default cwd)
--port, -p base port to attempt (default 8080)
--host, -h host name (default localhost)
--css, -s optional path to a CSS file
--index, -i HTML file to serve as index.html
API
server = httpServer([opt], [cb])
Creates a new HTTP server with the specified options and optional callback. Returns the server
instance.
Options:
cwd
(String|undefined)- the base directory to resolve file paths, defaults to
process.cwd()
- the base directory to resolve file paths, defaults to
entry
(String|Function|undefined)- a JavaScript entry file path, relative to
cwd
- or, can be a
middleware(req, res)
function - if not specified, no
<script>
will be added to the HTML
- a JavaScript entry file path, relative to
index
(String|Function|undefined)- defaults to a bare-bones HTML index
- the HTML index file path, relative to
cwd
- or, can be a
middleware(req, res, ev)
function- the
ev
parameter holds the{ title, entry }
strings
- the
root
(String|undefined)- the root directory to serve static content form, defaults to
cwd
- the root directory to serve static content form, defaults to
port
(Number|undefined)- the base port to start searching from, default
8080
- the base port to start searching from, default
host
(String|undefined)- the host name, default
'localhost'
- the host name, default
title
(String|undefined)- the HTML title, if unspecified no
<title>
will be written
- the HTML title, if unspecified no
css
(String|undefined)- an optional href path for a
<link rel="stylesheet">
tag in the<head>
- relative to
root
since it is just an href in the tag - only gets applied to the default
index
handler
- an optional href path for a
If you specify a function for entry
, the script will serve 'index.js'
by default. This pathname be changed with opt.src
.
The callback takes the form callback(err, ev)
with the following event parameters when the server starts listening:
url: String // 'http://localhost:8080/' port: Number // 8080 host: String // 'localhost'
License
MIT, see LICENSE.md for details.