Skip to content

Instantly share code, notes, and snippets.

@futoase
Created May 17, 2013 05:32
Show Gist options
  • Select an option

  • Save futoase/5597134 to your computer and use it in GitHub Desktop.

Select an option

Save futoase/5597134 to your computer and use it in GitHub Desktop.
YouTubeプレイヤーを利用する時にFlashPlayerからJavaScriptのfunctionをフックするにはローカルじゃあダメってことで ローカルで静的ファイル配信するの用意した。ありそうだけど。
var http = require('http')
, url = require('url')
, fs = require('fs')
, path = require('path')
, mime = require('mime');
HTTP_PORT = 8080;
ROOT_REAL_PATH = path.dirname(__filename);
INDEX_FILE_NAME = "index.html";
http.createServer(function(req, res){
fs.realpath("." + req.url, function(err, resolvedPath) {
if (err) { console.log( "File not found: " + err ); return; }
fs.exists(resolvedPath, function(exists) {
var contentsPath = resolvedPath;
if (!exists || resolvedPath == ROOT_REAL_PATH) {
contentsPath += '/' + INDEX_FILE_NAME;
}
fs.readFile(contentsPath, function(err, data){
var type = mime.lookup(contentsPath);
res.writeHead(200, { 'Content-Type': type });
res.end(data);
});
});
});
}).listen(HTTP_PORT);
console.log('Starting server. post is: ' + HTTP_PORT);
@futoase

futoase commented May 17, 2013

Copy link
Copy Markdown
Author

postとは...

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