开篇引用网上的说法:
简单的说 Node.js 就是运行在服务端的 JavaScript。Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。
关于常见的五种I/O模型,大家可以自行搜索了解。
下面的示例,是基于NodeJS+express而做的。
NodeJs环境的安装在此不做陈述。下面假设你已经安装了NodeJS环境。然后通过
npm install express --save
示例代码如下:
var mysql=require('mysql');
var express=require('express');
var app=express();
app.get('/getCode',function(req,res){
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '******',
database : 'codedb'
});
connection.connect();
connection.query('SELECT * from sfk_acm_code', function (error, results, fields)
{
if (error) throw error;
for(var i in results)
{
console.log('The solution is: ', results[i]['code']);
}
res.send(results);
});
connection.end();
});
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})
执行命令:
node mysql.js
注意:要定位到mysql.js所在的目录,然后执行node命里。
访问:http://127.0.0.1:8080/getCode,结果如下:
[{"id":2,"code":"一级建造师\nXXXXX-XXXXXXXXX-XX","subject_id":6,"state":1,"created_by":"admin","created_on":"2019-02-19T06:42:13.035Z","modify_by":"admin","modify_on":"2019-02-23T08:41:58.495Z","del":0},{"id":3,"code":"一级建造师\nXXXXX-XXXXXXXXX-XX\n二级建造师\nABCIEF890-XUIWEJF-CFDIFQW","subject_id":6,"state":0,"created_by":"admin","created_on":"2019-02-19T06:42:45.511Z","modify_by":"admin","modify_on":"2019-02-19T08:11:02.018Z","del":1},{"id":4,"code":"造价工程师历年真题\nXXXXX-XXXXXXXXX-XXXXX\n消防工程师\nXXXXX-XXXXXXXXXX-XXXXX","subject_id":6,"state":1,"created_by":"admin","created_on":"2019-02-19T08:04:22.776Z","modify_by":"admin","modify_on":"2019-02-23T08:42:27.897Z","del":0},{"id":5,"code":"注册消防工程师\nXXXXXXXXXXXX-XXXXXXXX-XXXXXXX","subject_id":5,"state":1,"created_by":"admin","created_on":"2019-02-19T08:59:15.200Z","modify_by":"admin","modify_on":"2019-02-23T08:52:46.093Z","del":0},{"id":6,"code":"一级建造师\nXXXX-XXX-XXDDCV\n二级建造师\nDFEF-UIDF-XIKFNW","subject_id":6,"state":0,"created_by":"admin","created_on":"2019-02-20T09:58:55.852Z","modify_by":"/Demand/Recycle","modify_on":"2019-02-23T09:07:01.074Z","del":0},{"id":7,"code":"一级建造师建设工程项目管理\n 5NKOX-5T1P2-N345J-FNUPM\n","subject_id":10,"state":1,"created_by":"admin","created_on":"2019-02-21T09:14:09.855Z","modify_by":"admin","modify_on":"2019-02-23T08:54:25.170Z","del":0},{"id":8,"code":"\n一级建造师建筑工程管理与实务\n 7CSVK-0H5CK-HOYGW-M3MS0","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-21T09:14:36.335Z","modify_by":"/Demand/Recycle","modify_on":"2019-02-23T06:17:57.914Z","del":0},{"id":9,"code":"vcxv","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T05:50:09.948Z","modify_by":"admin","modify_on":"2019-02-26T05:50:09.948Z","del":0},{"id":10,"code":"cccvxc","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T05:50:39.086Z","modify_by":"admin","modify_on":"2019-02-26T05:50:39.086Z","del":0},{"id":11,"code":"fdsf","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T05:51:45.386Z","modify_by":"admin","modify_on":"2019-02-26T05:51:45.386Z","del":0},{"id":12,"code":"dfds","subject_id":10,"state":0,"created_by":"admin","created_on":"2019-02-26T05:52:19.586Z","modify_by":"admin","modify_on":"2019-02-26T05:52:19.586Z","del":0},{"id":13,"code":"dfsf","subject_id":10,"state":0,"created_by":"admin","created_on":"2019-02-26T05:53:12.713Z","modify_by":"admin","modify_on":"2019-02-26T05:53:12.713Z","del":0},{"id":14,"code":"dfdsf","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T05:54:34.497Z","modify_by":"admin","modify_on":"2019-02-26T05:54:34.497Z","del":0},{"id":15,"code":"fdsfads","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T05:55:22.131Z","modify_by":"admin","modify_on":"2019-02-26T05:55:22.131Z","del":0},{"id":16,"code":"fdefasdf","subject_id":10,"state":0,"created_by":"admin","created_on":"2019-02-26T05:55:35.520Z","modify_by":"admin","modify_on":"2019-02-26T05:55:35.520Z","del":0},{"id":17,"code":"fdsfds","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T05:56:01.081Z","modify_by":"admin","modify_on":"2019-02-26T05:56:01.081Z","del":0},{"id":18,"code":"fdsfsd","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T05:57:05.578Z","modify_by":"admin","modify_on":"2019-02-26T05:57:05.578Z","del":0},{"id":19,"code":"fdsf","subject_id":10,"state":0,"created_by":"admin","created_on":"2019-02-26T05:57:27.338Z","modify_by":"admin","modify_on":"2019-02-26T05:57:27.338Z","del":0},{"id":20,"code":"dd","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T05:58:15.470Z","modify_by":"admin","modify_on":"2019-02-26T05:58:15.470Z","del":0},{"id":21,"code":"fsdfs","subject_id":10,"state":0,"created_by":"admin","created_on":"2019-02-26T05:58:54.686Z","modify_by":"admin","modify_on":"2019-02-26T05:58:54.686Z","del":0},{"id":22,"code":"fdsfds","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T06:00:28.632Z","modify_by":"admin","modify_on":"2019-02-26T06:00:28.632Z","del":0},{"id":23,"code":"fsdfsd","subject_id":12,"state":0,"created_by":"admin","created_on":"2019-02-26T06:00:40.941Z","modify_by":"admin","modify_on":"2019-02-26T06:00:40.941Z","del":0},{"id":24,"code":"fdsafsdaf","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T06:01:32.405Z","modify_by":"admin","modify_on":"2019-02-26T06:01:32.405Z","del":0},{"id":25,"code":"34rsdfsdaf","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T06:01:36.674Z","modify_by":"admin","modify_on":"2019-02-26T06:01:36.674Z","del":0},{"id":26,"code":"fdsafsdfd","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T06:01:40.462Z","modify_by":"admin","modify_on":"2019-02-26T06:01:40.462Z","del":0},{"id":27,"code":"fdsfsdf","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T06:01:43.470Z","modify_by":"admin","modify_on":"2019-02-26T06:01:43.470Z","del":0},{"id":28,"code":"fdsfds","subject_id":11,"state":0,"created_by":"admin","created_on":"2019-02-26T06:02:21.008Z","modify_by":"admin","modify_on":"2019-02-26T06:02:21.008Z","del":0}]