首页 > 代码库 > node.js如何读取MySQL数据
node.js如何读取MySQL数据
先安装mysql模块。
node.js默认安装时,模块文件放在 /usr/local/lib/node_modules 这个目录下,为了便宜管理,模块还是统一安装到这里好。
$ cd /usr/local/lib
$ npm install mysql
程序文件mysql.js
var Client = require(‘/usr/local/lib/node_modules/mysql‘).Client;
var client = new Client();
client.user = ‘root‘;
client.password = ‘‘;
console.log(‘Connecting to MySQL...‘);
client.query(‘USE tiny_shop‘); //如果MySQL中没有库表,赶紧建。
http = require("http");
var server = http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
client.query(‘SELECT * FROM tags‘, function selectCb(err, results, fields) {
if (err) {
throw err;
}
var data = http://www.mamicode.com/‘‘;"util");
sys.puts("Server running at http://localhost:8080/");
运行
$ node mysql.js
在浏览器里,输入 http://localhost:8080 ,就能显示数据库里的数据了。
node.js如何读取MySQL数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。