gpt4 book ai didi

mysql - HTML5页面用node.js连接mysql数据库

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:24 24 4
gpt4 key购买 nike

我已尝试使用 Joel 解决方案,但遇到了一些错误。谁能告诉我问题出在哪里。

应用程序.js

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, mysql = require('mysql')

var client = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
});

client.connect();

app.listen(3232);

function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}

res.writeHead(200);
res.end(data);
});
}

io.sockets.on('connection', function (socket) {
socket.on('login', function(usr, pass){
var TEST_DATABASE = 'mysqltest';
var TEST_TABLE = 'users';

client.query('USE '+TEST_DATABASE);

client.query('SELECT name FROM '+TEST_TABLE+' WHERE user = "'+usr+'" AND password = "'+pass+'"', function(err, results) {
if (err) throw err;
console.log(results[0]); // [{1: 1}]
socket.emit('retuLogIn',results[0]['name']);
});

});
socket.on('disconnect', function(){
console.log('Server has disconnected');
});
});

index.html

<html>
<title>WebSocket Client Demo [socket.io]</title>
<script src="http://localhost:3232/socket.io/socket.io.js"></script>
<script>
function connect() {

try
{
var socket = io.connect('http://localhost:3232/');
socket.on("connect",function(){
document.getElementById('status').innerHTML ="Browser has connected to the app server";
socket.emit('login', document.getElementById('txtUser').value,
document.getElementById('txtPass').value);

});
socket.on('retuLogIn', function (data) {
document.getElementById('status').innerHTML = 'Welcome '+data;
});
}
catch(err)
{
document.getElementById('status').innerHTML = err.message;
}
}
</script>
<body>
<h1>WebSocket Client Demo</h1>
<div><p id="status">Enter user and password to Log-In</p></div>
<label>User :</label>
<input id="txtUser" type="text" maxlength="10" />
<label>Password :</label>
<input id="txtPass" type="text" maxlength="10" />
<button id="connect" onClick='connect()'/>Log-In</button>
</body>
</html>

当我尝试在 node.js 中运行 app.js 文件时,它会给我以下错误:

C:\Program Files (x86)\nodejs>node "C:\Program Files (x86)\nodejs\app.js" info - socket.io started

C:\Program Files (x86)\nodejs\app.js:6 var client = mysql.createConnection({ ^ TypeError: Object # has no method 'createConnection' at Object. (C:\Program Files (x86)\nodejs\app.js:6:24) at Module._compile (module.js:449:26) at Object..js (module.js:467:10) at Module.load (module.js:356:32) at Function._load (module.js:312:12) at module.js:487:10 at EventEmitter._tickCallback (node.js:238:9)

C:\Program Files (x86)\nodejs>

我不明白问题出在哪里。我已经检查安装了 mysql 模块。

请帮助解决问题。

最佳答案

@钱丹,我有同样的错误(简要地)

TypeError: Object #<Object> has no method 'createConnection'

检查 https://github.com/felixge/node-mysql但没有其他人将其报告为问题。安装最新(alpha)版的 node mysql 模块解决了这个问题:

npm install mysql@2.0.0-alpha3

YMMV。如果您在没有更新 mysql 模块的情况下解决了这个问题,请通知我们。

关于mysql - HTML5页面用node.js连接mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11646942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com