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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| var PORT = 1337, DIR = '.';
var http = require('http'), url = require('url'), fs = require('fs'), path = require('path'), ejs = require('ejs'); ejs.delimiter = '$$'; var mine = { "css": "text/css", "gif": "image/gif", "html": "text/html", "ico": "image/x-icon", "xml": "text/xml", "jpeg": "image/jpeg", "jpg": "image/jpeg", "js": "text/javascript", "json": "application/json", "pdf": "application/pdf", "png": "image/png", "svg": "image/svg+xml", "swf": "application/x-shockwave-flash", "tiff": "image/tiff", "txt": "text/plain", "wav": "audio/x-wav", "wma": "audio/x-ms-wma", "wmv": "video/x-ms-wmv" };
var server = http.createServer(function (req, res) { var pathname = url.parse(req.url).pathname; var realPath = path.join(DIR, pathname);
var ext = path.extname(realPath); ext = ext ? ext.slice(1) : 'unknown'; fs.exists(realPath, function (exists) { if (!exists) { var data={}; if(req.url == "/common/user/login"){ data={ rlt_code:'HH0000', rlt_msg:'成功', data:{ access_token:'token_zjf', open_id:'open_id123' } }; res.writeHead(200, { "Content-Type": "application/json;charset=UTF-8" }); res.end(JSON.stringify(data)); }else if(req.url == "/common/user/register"){ data={ rlt_code:'HH0000', rlt_msg:'成功' }; res.writeHead(200, { "Content-Type": "application/json;charset=UTF-8" }); res.end(JSON.stringify(data)); }
else{ res.writeHead(404, { "Content-Type": "text/plain" }); res.end("404 error! File not found.!"); }
} else { if(ext=='html'){
fs.readFile(realPath, "utf-8", function (err, file) {
var flag=true; while (flag){ var start=file.indexOf('<*-'), end=0; if(start>=0) { end = file.indexOf('-*>'); var str = file.substring(start, end + 3); var str2=str.substring(3,str.length-3); var str2Arr=str2.split('||||'); str2Arr[0]=str2Arr[0].replace(/(^\s*)|(\s*$)/g, ""); str2Arr[1]=str2Arr[1].replace(/(^\s*)|(\s*$)/g, ""); var str1=fs.readFileSync(__dirname + str2Arr[0], 'utf8'); var o={}; try{ o=JSON.parse(str2Arr[1]); var returnStr=ejs.render(str1,o); file=file.replace(str,returnStr); }catch(e){ console.log('模板入参转换json出错'); res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end('模板入参转换json出错','utf8'); break; }
}else flag=false; }
if (err) { res.writeHead(500, { 'Content-Type': 'text/plain' }); res.end(err); } else { var contentType = mine[ext] || "text/plain"; res.writeHead(200, { 'Content-Type': contentType }); res.write(file, "utf-8"); res.end(); } }); }else{ fs.readFile(realPath, "binary", function (err, file) { if (err) { res.writeHead(500, { 'Content-Type': 'text/plain' }); res.end(err); } else { var contentType = mine[ext] || "text/plain"; res.writeHead(200, { 'Content-Type': contentType }); res.write(file, "binary"); res.end(); } }); }
} });
}); server.listen(PORT); console.log("Server runing at port: " + PORT + ".");
|