Skip to content

Instantly share code, notes, and snippets.

@usagi
Created June 28, 2012 02:03
Show Gist options
  • Select an option

  • Save usagi/3008268 to your computer and use it in GitHub Desktop.

Select an option

Save usagi/3008268 to your computer and use it in GitHub Desktop.
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();
});
});
};
@usagi
Copy link
Copy Markdown
Author

usagi commented Jun 28, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment