server.js 1.01 KB
var http = require('http');
var fs = require('fs');
var url = require('url');
 
 
// 创建服务器
http.createServer( function (request, response) {  
   
   // 解析请求,包括文件名
   var pathname = url.parse(request.url).pathname;
   
   // 输出请求的文件名
//    console.log("Request for " + pathname + " received.");
   
   // 从文件系统中读取请求的文件内容
   fs.readFile(pathname.substr(1), function (err, data) {
      if (err) {
         console.log(err);
         // Content Type: text/plain
         response.writeHead(404, {'Content-Type': 'text/html'});
      }else{         
         // Content Type: text/plain
        // response.writeHead(200,{'Content-Type': 'text/css'}); //会导致css js等不加载 文本设置问题   
         
         // 响应文件内容
         response.write(data);        
      }
      //  发送响应数据
      response.end();
   });   
}).listen(9999);
 
// 控制台会输出以下信息
console.log('Server running at http://127.0.0.1:9999/');