This repository has been archived by the owner on Jul 22, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tnantoka
committed
Oct 1, 2011
0 parents
commit 3724651
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Mac OS X automatic create files | ||
.DS_Store | ||
|
||
# Vim automatic create files | ||
*~ | ||
.*.sw* | ||
|
||
# Temporary files | ||
|
||
# npm modules | ||
node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# public.js | ||
|
||
Run static file hosting server with specified public dir & port. | ||
Support a "direcotry index" like Apache httpd. | ||
|
||
## Usage | ||
|
||
# install | ||
npm install -g public | ||
|
||
# start server | ||
public | ||
|
||
# start server with option | ||
public test 3030 | ||
Public.js server running with "/path/to/test" on port 3030 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#!/usr/bin/env node | ||
|
||
/* Load modules */ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var url = require('url'); | ||
var http = require('http'); | ||
var mime = require('mime'); | ||
|
||
/* Load package.json */ | ||
var package = JSON.parse(fs.readFileSync(__dirname + '/../package.json')); | ||
|
||
/* Define constants */ | ||
var VERSION = package.version; | ||
var DEFAULT_DIR = '.'; | ||
var DEFAULT_PORT = 3000; | ||
|
||
var USAGE = '' | ||
+ 'Usage: publicjs [options] [dir="' + DEFAULT_DIR + '"] [port="' + DEFAULT_PORT + '"]\n' | ||
+ '\n' | ||
+ 'Options:\n' | ||
+ '\t-v, --version\tShow version number\n' | ||
+ '\t-h, --help\tShow this help message\n' | ||
; | ||
|
||
/* Define functions */ | ||
|
||
// Successful end | ||
function success(msg) { | ||
console.log(msg); | ||
process.exit(0); | ||
} | ||
|
||
// Abnormal end | ||
function error(msg) { | ||
console.error(msg); | ||
process.exit(1); | ||
} | ||
|
||
/* Parse arguments */ | ||
var args = process.argv.slice(2); | ||
var dir = DEFAULT_DIR; | ||
var port = DEFAULT_PORT; | ||
switch (args[0]) { | ||
case '-h': | ||
case '--help': | ||
success(USAGE); | ||
break; | ||
case '-v': | ||
case '--version': | ||
success(VERSION); | ||
break; | ||
default: | ||
// publicjs dir port | ||
if (args.length >= 2) { | ||
dir = args[0]; | ||
if (isFinite(args[1])) { | ||
port = args[1]; | ||
} | ||
} else if (args.length == 1) { | ||
// publicjs port | ||
if (isFinite(args[0])) { | ||
port = args[0]; | ||
// publicjs dir | ||
} else { | ||
dir = args[0]; | ||
} | ||
} | ||
} | ||
|
||
if (path) { | ||
http.createServer(function(req, res) { | ||
var pathname = url.parse(req.url).pathname; | ||
var filePath = path.join(dir, pathname); // Real file path | ||
var base = filePath.replace(dir, ''); // Base path for browser link | ||
var abs = path.resolve(filePath); | ||
console.log(new Date().toString(), abs); | ||
fs.readFile(filePath, function(err, data) { | ||
if (err) { | ||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
// Index files if 404 or directory | ||
if ([2, 21].indexOf(err.errno) < 0) { | ||
res.write(['<h1>', res.message, '</h1>'].join('')); | ||
res.end(['<pre>', JSON.stringify(err, null, ' '), '</pre>'].join('')); | ||
return; | ||
} else { | ||
if (err.errno == 2) { | ||
filePath = path.dirname(filePath); | ||
} | ||
fs.readdir(filePath, function(err, files) { | ||
if (err) { | ||
res.write(['<h1>', res.message, '</h1>'].join('')); | ||
res.end(['<pre>', JSON.stringify(err, null, ' '), '</pre>'].join('')); | ||
return; | ||
} | ||
var list = ['<ul>']; | ||
files.unshift('..'); | ||
files.forEach(function(file) { | ||
list.push('<li><a href="', path.join(base, file),'">', file, '</a></li>'); | ||
}); | ||
list.push('</ul>'); | ||
res.write(['<h1>', 'Index of "', abs, '"</h1>'].join('')); | ||
res.end(list.join('\n')); | ||
}); | ||
} | ||
return; | ||
} | ||
res.writeHead(200, { 'Content-Type': mime.lookup(filePath) }); | ||
res.end(data); | ||
}); | ||
}).listen(port, function() { | ||
console.log('Public.js server running with "' + path.resolve(dir) + '" on port ' + port); | ||
}); | ||
} else { | ||
error(USAGE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"author": "tnantoka <bornneet@livedoor.com> (http://blog.bornneet.com/)", | ||
"name": "public", | ||
"description": "Run http server hosting static files with specified public dir & port", | ||
"version": "0.1.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/tnantoka/public.git" | ||
}, | ||
"engines": { | ||
"node": ">= 0.4.11" | ||
}, | ||
"dependencies": { | ||
"mime": "~1.2.4" | ||
}, | ||
"devDependencies": {}, | ||
"bin": { | ||
"public": "bin/public" | ||
} | ||
} |