GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — SolarSystemThreeJs/server.js at master · Ryanho84/SolarSystemThreeJs Expand file tree/ Copy pathserver.jsMore file actions 49 lines (35 loc) · 1.32 KB / Copy pathserver.jsFile metadata and controls49 lines (35 loc) · 1.32 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 //http.js var http = require('http'); var fs = require('fs');//引入文件读取模块 var documentRoot = 'D:/workspace/js/solarSystem/app'; //需要访问的文件的存放目录,可根据自己存放的目录修改 var server= http.createServer(function(req,res){ var url = req.url; //客户端输入的url,例如如果输入localhost:8888/index.html //那么这里的url == /index.html var file = documentRoot + url; console.log(url); //E:/PhpProject/html5/websocket/www/index.html fs.readFile( file , function(err,data){ /* 一参为文件路径 二参为回调函数 回调函数的一参为读取错误返回的信息,返回空就没有错误 二参为读取成功返回的文本内容 */ if(err){ res.writeHeader(404,{ 'content-type' : 'text/html;charset="utf-8"' }); res.write('<h1>404错误</h1><p>你要找的页面不存在</p>'); res.end(); }else{ res.writeHeader(200,{ 'content-type' : 'text/html;charset="utf-8"' }); res.write(data);//将index.html显示在客户端 res.end(); } }); }).listen(8888); console.log('服务器开启成功'); |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |