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
| var PORT = 1337, DIR = '.';
var http = require('http'), url = require('url'), fs = require('fs'), path = require('path'); var mine = { "css": "text/css", "gif": "image/gif", "html": "text/html", "ico": "image/x-icon", "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", "xml": "text/xml" };
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) {
if(req.url == "/common/user/login"){ var 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"){ var 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 { 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 + ".");
|