【发布时间】:2018-03-27 04:50:20
【问题描述】:
我对 NodeJS 编码完全陌生,所以为了让自己更具挑战性,我还尝试通过“node-orm2”使用 ORM(除非有人可以建议更简单的 orm 框架?)
我使用的是 Ubuntu 服务器,它还安装了 MySQL、Apache 和 Tomcat - 所以我希望 Node 应用程序监听端口 3000; (Apache 在 80 端口,而 Tomcat 在 8080 端口)。
我已经安装了 MySQL Driver + ORM 模块,并且它们正确地位于“node_modules”文件夹中 - 这是我项目的子文件夹。 MySQL 的设置很简单,用户名:“user”和密码:“password”
当我运行应用程序时,我收到错误消息:
“连接错误:错误:连接 ECONNREFUSED 127.0.0.1:3306”
哦 - 应用程序只是尝试将记录写入数据库“测试”,如果成功,它只是将消息“成功”返回给浏览器。
这是我目前的代码;
var http = require('http');
var orm = require('orm');
var message;
orm.connect('mysql://user:password@localhost/Test', function(err, db) {
if (err) return console.error('Connection error: ' + err);
var Test = db.define("test", {
User : String,
Num : Number,
Address : String,
Phone : Number,
State : String
});
var newRecord = {};
newRecord.User = 'Username';
newRecord.Num = Math.random();
newRecord.Address = '10 Idiot St, Somewhere';
newRecord.Phone = 12345678;
newRecord.State = 'VIC';
orm.create(newRecord, function(err, results) {
if (err) return console.console.error('DBError: ' + err);
});
message = 'Success';
});
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type' : 'text/plain'});
res.end('Response: ' + message);
}).listen(3000, "192.168.0.127");
console.log('Server running at http://192.168.0.127:3000/');
【问题讨论】:
-
您的 3306 端口是否打开或被防火墙阻止?