Browse Source

updated deps, better error handling, compression

master
Lloyd Brookes 11 years ago
parent
commit
b3bb524122
  1. 9
      package.json
  2. 33
      ws.js

9
package.json

@ -1,15 +1,14 @@
{ {
"name": "local-web-server", "name": "local-web-server",
"version": "0.1.5", "version": "0.1.5",
"description": "Local web server",
"description": "Launch a lightweight static web server. Zero configuration.",
"bin": { "bin": {
"ws": "./server.js"
"ws": "./ws.js"
}, },
"repository": "https://github.com/75lb/local-web-server", "repository": "https://github.com/75lb/local-web-server",
"author": "Lloyd Brookes", "author": "Lloyd Brookes",
"license": "BSD",
"dependencies": { "dependencies": {
"connect": "~2.11.0",
"nature": "~0.1.0"
"connect": "~2.12.0",
"nature": "~0.2.0"
} }
} }

33
server.js → ws.js

@ -1,17 +1,28 @@
#!/usr/bin/env node #!/usr/bin/env node
var connect = require('connect'),
var connect = require("connect"),
http = require("http"),
Thing = require("nature").Thing; Thing = require("nature").Thing;
var usage = "usage: ws [--port|-p <port>] [--log-format|-p dev|default|short|tiny]";
function red(txt){ return "\x1b[31m" + txt + "\x1b[0m"; }
function green(txt){ return "\x1b[32m" + txt + "\x1b[0m"; }
function halt(message){
console.log(red("Error ") + message);
console.log(usage);
process.exit(1);
}
var usage = "usage: ws [--port|-p <port>] [--log-format|-f dev|default|short|tiny]";
var options = new Thing() var options = new Thing()
.define({ name: "port", alias: "p", type: "number", defaultOption: true, default: 8000 }) .define({ name: "port", alias: "p", type: "number", defaultOption: true, default: 8000 })
.define({ name: "log-format", alias: "f", type: "string", default: "dev" }) .define({ name: "log-format", alias: "f", type: "string", default: "dev" })
.define({ name: "help", alias: "h", type: "boolean" }) .define({ name: "help", alias: "h", type: "boolean" })
.on("error", function(err){
halt(err.message);
})
.set(process.argv); .set(process.argv);
if (!options.valid){ if (!options.valid){
console.log(usage);
throw new Error(options.validationMessages);
halt(options.validationMessages)
} else if (options.help){ } else if (options.help){
console.log(usage); console.log(usage);
@ -26,10 +37,20 @@ if (!options.valid){
.replace("GMT", "").replace(" (BST)", ""); .replace("GMT", "").replace(" (BST)", "");
}); });
connect()
var app = connect()
.use(connect.logger(options["log-format"])) .use(connect.logger(options["log-format"]))
.use(connect.compress())
.use(connect.static(process.cwd())) .use(connect.static(process.cwd()))
.use(connect.directory(process.cwd()))
.use(connect.directory(process.cwd(), { icons: true }));
http.createServer(app)
.on("error", function(err){
if (err.code === "EADDRINUSE"){
halt("port " + options.port + " is already is use")
} else {
halt(err.message);
}
})
.listen(options.port); .listen(options.port);
process.stderr.write("serving at http://localhost:" + options.port + "\n"); process.stderr.write("serving at http://localhost:" + options.port + "\n");
Loading…
Cancel
Save