example.js 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. /* 注册命名空间 */
  2. var us = {};
  3. /* 引用外部模块 */
  4. var app = require('./lib/main.js');
  5. us.mysql = require('./mysql.js');
  6. us.querystring = require('querystring');
  7. //以当前目录下的demo目录为web应用根目录
  8. app.baseUrl(__dirname);
  9. app.use('/', function (req, res) {
  10. res.writeHead(200, { "Content-Type": "text/html;application/json;charset=utf-8", 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With' });
  11. debugger;
  12. //querystring.parse可以把形如mode=select&test=1111的字符串拆分成一个json对象,不过这个对象很奇怪,直接输出这个对象会报错
  13. var _postJson = req.body || req.query; //获取前台数据
  14. var _usparams = _postJson.mode; //前台传递进来的插入删除等模式
  15. //console.dir(_usparams);
  16. if (_usparams) {
  17. _usparams = _usparams.split(","); //作为一个数组传递,不再拆分,可以让后台极度简化
  18. //"usgetclientip"
  19. //console.log(params);
  20. // console.log(_usparams);
  21. if (_usparams[2] == "usipaddress") { res.end(req.connection.remoteAddress); } //获取访问者IP
  22. us.mysql.usselect(_usparams, function (results) { res.end(results); }); //执行Mysql
  23. }
  24. });
  25. app.listen(4444, "nodejs.1473.cn");
  26. console.log("服务启动成功");