|
@@ -0,0 +1,76 @@
|
|
|
|
|
+
|
|
|
|
|
+var us = {}; //注册命名空间。
|
|
|
|
|
+//----------------------------------------引用包含系统模块---------------------------------
|
|
|
|
|
+us.http = require('http'); //引用包含http处理类
|
|
|
|
|
+us.querystring = require("querystring"); //引用获取参数类
|
|
|
|
|
+us.url = require("url"); //引用url地址处理类
|
|
|
|
|
+//-----------------------------------------引用包含用户模块---------------------------------
|
|
|
|
|
+us._usdb = require('./us.db.js'); //包含另外一个文件
|
|
|
|
|
+us.mongodb = require('./us.mongodb.js'); //引用mongodb数据库文件
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+//-------------------引入express模块,如果此处出错,请确认express已安装,------------------
|
|
|
|
|
+//而且express在环境变量NODE_PATH目录中
|
|
|
|
|
+var express = require('express');
|
|
|
|
|
+//创建web服务
|
|
|
|
|
+var app = express();
|
|
|
|
|
+//以当前目录下的demo目录为web应用根目录
|
|
|
|
|
+app.use(express.static(__dirname + '/'));
|
|
|
|
|
+//显示错误信息,以方便调试
|
|
|
|
|
+app.use(express.errorHandler({
|
|
|
|
|
+ showStack: true,
|
|
|
|
|
+ dumpExceptions: true
|
|
|
|
|
+}));
|
|
|
|
|
+
|
|
|
|
|
+app.all("*", function (req, res) {
|
|
|
|
|
+ //备注:res.writeHead(200, {'Content-Type': 'text/plain'});//英文编码
|
|
|
|
|
+ //备注:传递表单需要添加,'application/x-www-form-urlencoded'
|
|
|
|
|
+ //备注:Access-Control-Allow-Origin解决ie10跨域,还未完全其他跨域,如chrome等,需要进一步完善。
|
|
|
|
|
+ //备注:"Content-Type": "text/html;charset=utf-8"解决nodejs连接mysql中文编码问题
|
|
|
|
|
+ //备注:'Access-Control-Allow-Origin': '*' 解决nodejs Access-Control-Allow-Origin不允许跨域调用的问题。现在用端口有这个问题,以后需要移除。
|
|
|
|
|
+ //console.log("ok");没有调试模式,只能用这个输出错误。
|
|
|
|
|
+ //下面的头查了大概1周时间,必须这样写。
|
|
|
|
|
+ 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' });
|
|
|
|
|
+
|
|
|
|
|
+ debugger;
|
|
|
|
|
+ //nodejs接收参数也要异步,编程起来就有点麻烦了,注意产生闭包情况。
|
|
|
|
|
+ //NodeJS引擎中许多对象都有预定的事件,如用户在发送http请求之后获得的http.ServerRequest对象就有data和end两个事件,
|
|
|
|
|
+ //其中data指接收到响应信息正文中的一部分时会触发此事件,end指完全接收完信息后都会触发一次。开发人员如果想处理响应,则需要注册回调函数
|
|
|
|
|
+ //on('data') 事件 只有post 请求才能触发,由于1473都是post,所以研究这一块,传统的get方式是不一样的。
|
|
|
|
|
+ var params = '';
|
|
|
|
|
+ //获取参数中
|
|
|
|
|
+ req.on('data', function (chunk) { params += chunk; });
|
|
|
|
|
+ //获取参数结束。
|
|
|
|
|
+ req.on('end', function () {
|
|
|
|
|
+ //querystring.parse可以把形如mode=select&test=1111的字符串拆分成一个json对象,不过这个对象很奇怪,直接输出这个对象会报错
|
|
|
|
|
+ var _postJson = us.querystring.parse(params);
|
|
|
|
|
+ console.log(_postJson);
|
|
|
|
|
+ //只能通过以下方式获取其中的值。这就有一定的局限性。。还需要研究。
|
|
|
|
|
+ var _mode = _postJson.mode; //前台传递进来的插入删除等模式
|
|
|
|
|
+ var _usjson = _postJson.usjson; //前台传递进来的参数
|
|
|
|
|
+ switch (_mode) {
|
|
|
|
|
+ case "select":
|
|
|
|
|
+ //访问mysql数据库
|
|
|
|
|
+ us._usdb.aaaaaa(function (results) { res.end(results); });
|
|
|
|
|
+ //us._usdb.usproselect("CALL PB_Select();", function (results) { res.end(results); });
|
|
|
|
|
+ //访问mongodb数据库
|
|
|
|
|
+ //us.mongodb.select("aaaaatest", function (results) { res.end(results); });
|
|
|
|
|
+ //访问mongodb存储过程
|
|
|
|
|
+ //us.mongodb.pro("test", function (results) { res.end(results); });
|
|
|
|
|
+ //us.mongodb.testparam("ttt", function (results) { res.end(results); });
|
|
|
|
|
+ break;
|
|
|
|
|
+ case "insert": res.end(_usjson); break;
|
|
|
|
|
+ default: res.end("default 1473"); break;
|
|
|
|
|
+ }
|
|
|
|
|
+ //res.end(_mode);
|
|
|
|
|
+ });
|
|
|
|
|
+ //备注第一个参数为构建连接字符串,第二个参数为异步查询数据库结束后调用的回调函数
|
|
|
|
|
+ //_usdb.usselect('SELECT * FROM ' + 'PBDirectory ' + 'limit 10', function (results) {if (results === 'false') { throw Error; } else { res.end(results);}});//加错误处理
|
|
|
|
|
+ //_usdb.usselect('SELECT * FROM ' + 'PBDirectory ' + 'limit 10', function (results) { res.end(results);});\
|
|
|
|
|
+ //res.end("\n node js end! ");
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+//启动express web服务,监听8080端口
|
|
|
|
|
+app.listen(1337);
|
|
|
|
|
+
|
|
|
|
|
+console.log('UseStudio Nodejs Express is Listening on http://nodejs.1473.cn:1337/:'); //提示
|