Browse Source

tidy up, ajax test

master
Lloyd Brookes 11 years ago
parent
commit
d3c80d4a8e
  1. 31
      bin/ws.js
  2. 2
      package.json
  3. 19
      test/ajax.html

31
bin/ws.js

@ -1,18 +1,18 @@
#!/usr/bin/env node #!/usr/bin/env node
"use strict"; "use strict";
var dope = require("console-dope"), var dope = require("console-dope"),
connect = require("connect"),
http = require("http"), http = require("http"),
cliArgs = require("command-line-args"), cliArgs = require("command-line-args"),
o = require("object-tools"), o = require("object-tools"),
path = require("path"), path = require("path"),
loadConfig = require("config-master"), loadConfig = require("config-master"),
homePath = require("home-path"),
logStats = require("stream-log-stats"),
connect = require("connect"),
morgan = require("morgan"), morgan = require("morgan"),
serveStatic = require("serve-static"), serveStatic = require("serve-static"),
directory = require("serve-index"), directory = require("serve-index"),
compress = require("compression"),
homePath = require("home-path"),
logStats = require("stream-log-stats");
compress = require("compression");
var usage = var usage =
"usage: \n\ "usage: \n\
@ -52,12 +52,14 @@ try {
halt(err.message); halt(err.message);
} }
/* override built-in defaults with stored config then command line args */
argv = o.extend({
var builtInDefaults = {
port: 8000, port: 8000,
directory: process.cwd(), directory: process.cwd(),
refreshRate: 500 refreshRate: 500
}, storedConfig, argv);
};
/* override built-in defaults with stored config and then command line args */
argv = o.extend(builtInDefaults, storedConfig, argv);
if (argv.config){ if (argv.config){
dope.log("Stored config: "); dope.log("Stored config: ");
@ -77,24 +79,25 @@ if (argv.config){
var app = connect(); var app = connect();
/* log using --log-format (if supplied) */ /* log using --log-format (if supplied) */
if(argv["log-format"]) {
if (argv["log-format"] === "none"){
var logFormat = argv["log-format"];
if(logFormat) {
if (logFormat === "none"){
// do nothing, no logging required // do nothing, no logging required
} else { } else {
if (argv["log-format"] === "logstalgia"){
if (logFormat === "logstalgia"){
/* customised logger :date token, purely to satisfy Logstalgia. */ /* customised logger :date token, purely to satisfy Logstalgia. */
morgan.token("date", function(){ morgan.token("date", function(){
var a = new Date(); var a = new Date();
return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString()) return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
.replace("GMT", "").replace(" (BST)", ""); .replace("GMT", "").replace(" (BST)", "");
}); });
argv["log-format"] = "default";
logFormat = "default";
} }
app.use(morgan(argv["log-format"]));
app.use(morgan(logFormat));
} }
/* if no specific `--log-format` required, pipe the default web log output
/* if no `--log-format` was specified, pipe the default format output
into `log-stats`, which prints statistics to the console */ into `log-stats`, which prints statistics to the console */
} else { } else {
app.use(morgan({ stream: logStats({ refreshRate: argv.refreshRate }) })); app.use(morgan({ stream: logStats({ refreshRate: argv.refreshRate }) }));
@ -118,7 +121,7 @@ if (argv.config){
}) })
.listen(argv.port); .listen(argv.port);
/* write status to stderr (stdout is reserved for web log output) */
/* write launch information to stderr (stdout is reserved for web log output) */
if (path.resolve(argv.directory) === process.cwd()){ if (path.resolve(argv.directory) === process.cwd()){
dope.error("serving at %underline{%s}", "http://localhost:" + argv.port); dope.error("serving at %underline{%s}", "http://localhost:" + argv.port);
} else { } else {

2
package.json

@ -1,7 +1,7 @@
{ {
"name": "local-web-server", "name": "local-web-server",
"version": "0.5.5", "version": "0.5.5",
"description": "Launch a lightweight static web server. Zero configuration.",
"description": "Lightweight static web server, zero configuration. Perfect for front-end devs.",
"bin": { "bin": {
"ws": "./bin/ws.js" "ws": "./bin/ws.js"
}, },

19
test/ajax.html

@ -0,0 +1,19 @@
<!DOCTYPE html>
<head>
<title>Ajax test</title>
</head>
<body>
<h1>README</h1>
<h2>loaded in the "Ajax" style</h2>
<pre id="readme"></pre>
<script>
var $ = document.querySelector.bind(document);
var req = new XMLHttpRequest();
req.open("get", "../README.md", true);
// req.open("get", "http://localhost:8010/README.md", true);
req.onload = function(){
$("#readme").textContent = this.responseText;
}
req.send()
</script>
</body>
Loading…
Cancel
Save