node.js-0.6をWindowsのネイティブ環境へインストール | アプレンティス プラクティス
例によってTINAMIのAPIを利用しています。
server.js
var http = require("http"), url = require("url"), path = require("path"), fs = require("fs"), parser = require('xml2json'), request = require('request'), xml = 'http://api.tinami.com/content/search?api_key=APIキー&text=%E3%83%9E%E3%83%8A%E3%82%8A%E3%81%A4', port = process.argv[2] || 8888; request(xml, function (error, response, body) { if (!error && response.statusCode == 200) { var options = { object: false, reversible: false, coerce: true, sanitize: true, trim: true, arrayNotation: false }; var json = parser.toJson(body, options); fs.writeFile('hoge.xml', json , function (err) { console.log(err); }); } }); 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 , filename = path.join(process.cwd(), uri); fs.exists(filename, function(exists){ console.log(filename+" "+exists); if (!exists) { Response["404"](); return ; } if (fs.statSync(filename).isDirectory()) { filename += '/hoge.xml'; } 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 );
$ node server.js