日期:2014-05-16  浏览次数:20347 次

node.js 读取文件
var sys = require("sys"),  
      http = require("http"),  
      url = require("url"),  
      path = require("path"),  
      fs = require("fs");  
 
  http.createServer(function(request, response) {  
      var uri = url.parse(request.url).pathname;  
         console.log(uri);
      var filename = path.join(process.cwd(), uri);  
          console.log(filename);
      path.exists(filename, function(exists) {  
          if(!exists) {  
              response.writeHeader(404, {"Content-Type": "text/plain"});  
              response.write("404 Not Found\n");  
              response.end();  
              return;  
          }  
    
          fs.readFile(filename, "binary", function(err, file) {  
              if(err) {  
                  response.writeHeader(500, {"Content-Type": "text/plain"});  
                  response.write(err + "\n");  
                  response.end();  
                  return;  
              }  
    
              response.writeHeader(200);  
              response.write(file, "binary");  
              response.end();  
          });  
      });  
  }).listen(8081);  
    
  sys.puts("Server running at http://localhost:8081/");