Permalink
Browse files

finished rudimentary xjsserver; releasing 0.1.2

  • Loading branch information...
tenorviol committed Apr 12, 2011
1 parent c097d77 commit ee630cc80e944de65ad53132c561cc60eed1d3c5
Showing with 100 additions and 29 deletions.
  1. +3 −2 HISTORY.md
  2. +5 −5 TODO.md
  3. +85 −17 bin/xjsserver
  4. +7 −5 package.json
View
@@ -1,7 +1,8 @@
-0.1.2 / (pending)
------------------
+0.1.2 / 2011-05-11
+------------------
* `render(response)` can now be a callback function or a writable stream
+* added command line xjsserver, a simple way of serving xjs
0.1.1 / 2011-04-11
-----------------
View
10 TODO.md
@@ -1,20 +1,20 @@
-0.1.2
+0.1.3
-----
-* accept callback function or writable stream for render response
-* automatic template/array loops
-* command line xjs server
+
future
------
+* automatic template/array loops
* seamless Express support
* when the file changes, reload the module
* inline xjs blocks
* auto conversion to html5, plus Schema checking
* exception stack trace line number
* namespace and custom tag support
-* tag error checking
+* tag error checking, read DTD/Schema
+* test xjsserver
* Which is better?
1. Writing many small strings to an http response stream.
View
@@ -1,30 +1,98 @@
#!/usr/bin/env node
-var http = require('http'),
- url = require('url'),
- path = require('path');
+var help = [
+ 'A very simple http server for rendering xjs',
+ '',
+ 'Options:',
+ ' -h, --help Show this information',
+ ' -p, --port arg Port number of http server',
+ ' --docroot arg Root directory of the http server',
+ ' --example Use the example directory as the docroot'
+].join('\n');
+
require('../lib/xjs');
+var path = require('path'),
+ fs = require('fs'),
+ http = require('http'),
+ url = require('url');
+
-var docroot = process.env.PWD;
+// option defaults
+var opts = {
+ port: 8000,
+ docroot: process.env.PWD
+};
-var server = http.createServer(function(req, res) {
- var fullpath = resolvePath(req);
- if (path.existsSync(fullpath)) {
- require(fullpath).render(res, { request : req });
- } else {
- res.end('404');
+// command line options
+var args = process.argv.slice(2);
+while (args.length) {
+ var arg = args.shift();
+ switch (arg) {
+ case '-h':
+ case '--help':
+ console.log(help);
+ return;
+ case '--docroot':
+ opts.docroot = args.shift();
+ break;
+ case '--example':
+ opts.docroot = __dirname + '/../example';
+ break;
+ case '-p':
+ case '--port':
+ opts.port = parseInt(args.shift());
+ break;
+ default:
+ console.log('unknown command line argument, "' + arg + '"');
}
-}).listen(8124, "127.0.0.1");
-console.log('Server running at http://127.0.0.1:8124/');
+}
-function resolvePath(req) {
+// check the port
+if (!opts.port) {
+ console.log('port must be a whole number');
+ return;
+}
+
+// check the docroot
+opts.docroot = path.resolve(opts.docroot);
+var valid_docroot = false;
+try {
+ var valid_docroot = fs.statSync(opts.docroot).isDirectory();
+} catch (err) {
+}
+if (!valid_docroot) {
+ console.log('invalid docroot, "' + opts.docroot + '"');
+ return;
+}
+
+
+function xjsServer(req, res) {
+ // resolve xjspath
var parts = url.parse(req.url);
var uri = path.normalize(parts.pathname);
- var fullpath = docroot + uri;
- var ext = path.extname(fullpath);
+ var xjspath = opts.docroot + uri;
+ var ext = path.extname(xjspath);
if (ext !== '.xjs') {
- fullpath += '.xjs';
+ xjspath += '.xjs';
}
- return fullpath;
+
+ renderXjs(xjspath, res, { request : req });
+}
+
+function renderXjs(xjspath, res, local) {
+ fs.stat(xjspath, function(err, stat) {
+ if (err) {
+ return res.end('404');
+ }
+ if (stat.isDirectory()) {
+ return renderXjs(xjspath + '/index.xjs', res, local);
+ }
+ require(xjspath).render(res, local);
+ });
}
+
+
+// start the server
+http.createServer(xjsServer).listen(opts.port);
+console.log('xjs server running on port ' + opts.port);
View
@@ -2,13 +2,15 @@
"name" : "xjs",
"description" : "A templating language for node.js using xml and javascript",
"keywords" : [ "template", "xml", "xhp" ],
- "version" : "0.1.1",
- "engines" : { "node" : "0.4" },
+ "version" : "0.1.2",
"author" : "Christopher Johnson",
+ "engines" : { "node" : "0.4" },
"main" : "lib/xjs.js",
- "directories" : { "lib" : "./lib" },
+ "bin": { "xjsserver": "./bin/xjsserver" },
"repository" :
- { "type" : "git", "url" : "git://github.com/tenorviol/xjs.git" },
+ { "type" : "git"
+ , "url" : "git://github.com/tenorviol/xjs.git" },
"licenses" :
- [ { "type" : "MIT", "url" : "http://github.com/tenorviol/xjs/raw/master/LICENSE" } ]
+ [ { "type" : "MIT"
+ , "url" : "http://github.com/tenorviol/xjs/raw/master/LICENSE" } ]
}

0 comments on commit ee630cc

Please sign in to comment.