首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

node.js 读取资料

2012-10-30 
node.js 读取文件var sys require(sys),http require(http),url require(url),path require

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/"); 

热点排行
Bad Request.