Created
June 28, 2012 02:03
-
-
Save usagi/3008268 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var http = require('http'), | |
| url = require('url'), | |
| path = require('path'), | |
| fs = require('fs'), | |
| var port_local = 8080; | |
| var load_static_file = function(uri, response) { | |
| // 相対パスで静的リソースを配信する。 | |
| var tmp = uri.split('.'); | |
| var type = tmp[tmp.length - 1]; | |
| var filename = path.join(process.cwd(), uri); | |
| path.exists(filename, function(exists) { | |
| if (!exists) { | |
| response.writeHead(404, {'Content-Type': 'text/plain'}); | |
| response.write('404 Not Found\n'); | |
| response.end(); | |
| return; | |
| } | |
| fs.readFile(filename, 'binary', function(err, file) { | |
| if (err) { | |
| response.writeHead(500, {'Content-Type': 'text/plain'}); | |
| response.write(err + '\n'); | |
| response.end(); | |
| return; | |
| } | |
| switch (type) { | |
| case 'html': | |
| response.writeHead(200, {'Content-Type': 'text/html'}); | |
| break; | |
| case 'js': | |
| response.writeHead(200, {'Content-Type': 'text/javascript'}); | |
| break; | |
| case 'css': | |
| response.writeHead(200, {'Content-Type': 'text/css'}); | |
| break; | |
| } | |
| response.write(file, 'binary'); | |
| response.end(); | |
| }); | |
| }); | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
copy from: http://d.hatena.ne.jp/Jxck/20101022/1287765155