Browse Source

port to connect 3.0, use config-master, docs

master
Lloyd Brookes 11 years ago
parent
commit
cfee473b78
  1. 6
      README.md
  2. 86
      bin/ws.js
  3. 19
      package.json

6
README.md

@ -1,4 +1,5 @@
[![NPM version](https://badge.fury.io/js/local-web-server.png)](http://badge.fury.io/js/local-web-server)
[![view on npm](http://img.shields.io/npm/v/local-web-server.svg)](https://www.npmjs.org/package/local-web-server)
![npm module downloads per month](http://img.shields.io/npm/dm/local-web-server.svg)
[![Dependency Status](https://david-dm.org/75lb/local-web-server.png)](https://david-dm.org/75lb/local-web-server)
![Analytics](https://ga-beacon.appspot.com/UA-27725889-12/local-web-server/README.md?pixel)
@ -119,6 +120,3 @@ servers:
parser: apache
color: 0.2, 0.2, 1.0, 1.0
```
[![NPM](https://nodei.co/npm-dl/local-web-server.png?months=3)](https://nodei.co/npm/local-web-server/)

86
ws.js → bin/ws.js

@ -4,9 +4,14 @@ var dope = require("console-dope"),
connect = require("connect"),
http = require("http"),
fs = require("fs"),
Thing = require("nature").Thing,
Model = require("nature").Model,
w = require("wodge"),
path = require("path");
path = require("path"),
loadConfig = require("config-master"),
morgan = require("morgan"),
serveStatic = require("serve-static"),
directory = require('serve-index'),
compress = require('compression');
var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny] [--compress|-c]";
@ -16,10 +21,8 @@ function halt(message){
process.exit(1);
}
/**
parse command-line args
*/
var argv = new Thing()
/* parse command-line args */
var argv = new Model()
.define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
.define({ name: "log-format", alias: "f", type: "string" })
.define({ name: "help", alias: "h", type: "boolean" })
@ -29,35 +32,20 @@ var argv = new Thing()
halt(err.message);
});
/*
Set default options from "package.json", ".local-web-server.json" or "~/.local-web-server.json", in that order
*/
var pkgPath = path.join(process.cwd(), "package.json"),
lwsPath = path.join(process.cwd(), ".local-web-server.json"),
homePath = path.join(w.getHomeDir(), ".local-web-server.json");
if (fs.existsSync(pkgPath)){
argv.set(require(pkgPath)["local-web-server"]);
}
if (fs.existsSync(lwsPath)){
argv.set(require(lwsPath));
}
if (fs.existsSync(homePath)){
argv.set(require(homePath));
}
/* Merge together options from "~/.local-web-server.json", "{cwd}/.local-web-server.json" and "{cwd}/package.json", in that order. */
var storedConfig = loadConfig(
path.join(w.getHomeDir(), ".local-web-server.json"),
path.join(process.cwd(), ".local-web-server.json"),
path.join(process.cwd(), "package.json:local-web-server")
);
argv.set(storedConfig);
/*
Finally, set the options from the command-line, overriding all defaults.
*/
/* Finally, set the options from the command-line, overriding all defaults. */
argv.set(process.argv);
/**
Die here if invalid args received
*/
/* Die here if invalid args received */
if (!argv.valid) halt(argv.validationMessages);
/**
$ ws --help
*/
if (argv.help){
dope.log(usage);
@ -74,10 +62,8 @@ if (argv.help){
process.exit(0);
});
/**
customised connect.logger :date token, purely to satisfy Logstalgia.
*/
connect.logger.token("date", function(){
/* customised logger :date token, purely to satisfy Logstalgia. */
morgan.token("date", function(){
var a = new Date();
return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
.replace("GMT", "").replace(" (BST)", "");
@ -85,11 +71,9 @@ if (argv.help){
var app = connect();
/*
log using --log-format (if supplied), else output statics
*/
/* log using --log-format (if supplied), else output statics */
if(argv["log-format"]){
app.use(connect.logger(argv["log-format"]));
app.use(morgan(argv["log-format"]));
} else {
app.use(function(req, res, next){
dope.column(1).write(++total.req);
@ -97,20 +81,14 @@ if (argv.help){
});
}
/**
--compress enables compression
*/
if (argv.compress) app.use(connect.compress());
/* --compress enables compression */
if (argv.compress) app.use(compress());
/**
static file server including directory browsing support
*/
app.use(connect.static(path.resolve(argv.directory)))
.use(connect.directory(path.resolve(argv.directory), { icons: true }));
/* static file server including directory browsing support */
app.use(serveStatic(path.resolve(argv.directory)))
.use(directory(path.resolve(argv.directory), { icons: true }));
/**
launch server
*/
/* launch server */
var server = http.createServer(app)
.on("error", function(err){
if (err.code === "EADDRINUSE"){
@ -121,18 +99,14 @@ if (argv.help){
})
.listen(argv.port);
/*
write status to stderr so stdout can be piped to disk ($ ws > log.txt)
*/
/* write status to stderr so stdout can be piped to disk ($ ws > log.txt) */
if (path.resolve(argv.directory) === process.cwd()){
dope.error("serving at %underline{%s}", "http://localhost:" + argv.port);
} else {
dope.error("serving %underline{%s} at %underline{%s}", argv.directory, "http://localhost:" + argv.port);
}
/**
in stats mode, monitor connections and bytes transferred
*/
/* in stats mode, monitor connections and bytes transferred */
if (!argv["log-format"]){
dope.hideCursor();
dope.log("%underline{Requests} %underline{Data} %underline{Connections}");

19
package.json

@ -3,14 +3,25 @@
"version": "0.3.7",
"description": "Launch a lightweight static web server. Zero configuration.",
"bin": {
"ws": "./ws.js"
"ws": "./bin/ws.js"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"lint": "jshint bin/ws.js; echo;"
},
"repository": "https://github.com/75lb/local-web-server",
"author": "Lloyd Brookes",
"dependencies": {
"connect": "~2.14.0",
"nature": "~0.3.0",
"connect": "^3.0.0",
"nature": "~0.4.0",
"console-dope": "~0.3.0",
"wodge": "~0.3.0"
"wodge": "~0.5.0",
"config-master": "~0.0.0",
"morgan": "^1.0.0",
"serve-static": "^1.1.0",
"serve-index": "^1.0.2",
"compression": "^1.0.2"
}
}
Loading…
Cancel
Save