Created
February 10, 2014 08:54
-
-
Save nobuhito/8912545 to your computer and use it in GitHub Desktop.
ローカルファイルをApacheのDirectoryIndexっぽく見せるNode.js
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
// based on http://shimz.me/blog/node-js/2690 | |
var scriptName = 'index.js'; | |
var separator = '___DATA___'; | |
var http = require('http'), | |
url = require('url'), | |
path = require('path'), | |
fs = require('fs'), | |
port = process.argv[2] || 8888; | |
http.createServer(function(request, response) { | |
var Response = { | |
'200': function(file, filename) { | |
var extname = path.extname(filename); | |
var header = { | |
'Access-Control-Allow-Origin': "*", | |
'Pragma': "no-cache", | |
'Cache-Control': "no-cache" | |
}; | |
response.writeHead(200, header); | |
response.write(file, "binary"); | |
response.end(); | |
}, | |
'404': function() { | |
response.writeHead(404, {'Content-Type': "text/plain"}); | |
response.write("404 Not Found\n"); | |
response.end(); | |
}, | |
'500': function(err) { | |
response.writeHead(500, {'Content-Type': "text/plain"}); | |
response.write(err + "\n"); | |
response.end(); | |
} | |
}; | |
var uri = url.parse(request.url).pathname; | |
var filename = path.join(process.cwd(), uri); | |
fs.exists(filename, function(exists) { | |
if (!exists) { Response["404"](); return; } | |
if (fs.statSync(filename).isDirectory()) { | |
// index.htmlがある場合はindex.htmlを表示し、それ以外はファイル一覧を表示 | |
// like a Apache DirectoryIndex | |
var files = fs.readdirSync(filename); | |
if (files.indexOf('index.html') == -1) { | |
var html = getDirectoryIndex(files); | |
Response["200"](html, 'auto'); | |
}; | |
filename += '/index.html'; | |
} | |
fs.readFile(filename, "binary", function(err, file) { | |
if (err) { Response["500"](err); return; } | |
Response["200"](file, filename); | |
}); | |
}); | |
}).listen(parseInt(port, 10)); | |
console.log("server running at http://localhost:" + port); | |
function getDirectoryIndex(files) { | |
var content = "<ul>\n"; | |
for (var i=0; i<files.length; i++) { | |
content += "<li>"; | |
content += "<a href=\"" + request.url + files[i] + "\">" + files[i] + "</a>"; | |
content += "</li>\n"; | |
} | |
content += "</ul>"; | |
var html = getHtml(content); | |
} | |
function getHtml(content) { | |
// TODO: 自分自身は自動的に拾いたい | |
var data = fs.readFileSync(path.join(process.cwd(), scriptName)); | |
var lines = data.toString().replace(/\r/g, '').split("\n"); | |
var template= ''; | |
var start = 0; | |
for (var i=0; i<lines.length; i++) { | |
var line = lines[i]; | |
if (line == separator) { start = 1; continue; } | |
if (start == 1) { | |
template += line + "\n"; | |
} | |
} | |
return template.replace('*/', '').replace(/%%content%%/, content); | |
} | |
/* | |
___DATA___ | |
<html> | |
<head> | |
</head> | |
<body> | |
%%content%% | |
</body> | |
</html> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment