bare-bones HTTP server with optional script entry
MIT License
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.
npm install serves [-g|--save]
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
A simple example:
var serves = require('serves')
var path = require('path')
serves({
cwd: process.cwd(),
root: 'public',
entry: 'src/index.js',
title: 'My Site'
}, function (err, ev) {
if (err) throw err
console.log('Listening on', ev.url)
})
Or see example/index.js, which mimics wzrd and bundles CommonJS on request.
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
server = httpServer([opt], [cb])
Creates a new HTTP server with the specified options and optional callback. Returns the server
instance.
Options:
cwd
(String|undefined)
process.cwd()
entry
(String|Function|undefined)
cwd
middleware(req, res)
function<script>
will be added to the HTMLindex
(String|Function|undefined)
cwd
middleware(req, res, ev)
function
ev
parameter holds the { title, entry }
stringsroot
(String|undefined)
cwd
port
(Number|undefined)
8080
host
(String|undefined)
'localhost'
title
(String|undefined)
<title>
will be writtencss
(String|undefined)
<link rel="stylesheet">
tag in the <head>
root
since it is just an href in the tagindex
handlerIf 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'
}
MIT, see LICENSE.md for details.