使用与nodejs
中的mysql数据库连接的openshift
创建了一个示例可伸缩应用程序。但我得到了502 error bad gateway
。#option httpchk GET
/ haproxy
。{/ p>
我有一个数据库 testnodejs 有表users(id int,name varchar)
;
我的示例代码 的index.html
<!doctype html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#submit').click(function () {
$.get('/users',data,function(data){
});
});
});
</script>
</head>
<body>
<h3>Enter a username</h3>
<input id="user" type="text" />
<input id="submit" type="submit" />
<p id="output"></p>
</body>
</html>
app.js
var express = require('express'),
mysql = require('mysql');
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
var app = module.exports = express.createServer();
var connection = mysql.createConnection({
host : process.env.OPENSHIFT_MYSQL_DB_HOST,
user : process.env.OPENSHIFT_MYSQL_DB_USERNAME,
password :process.env.OPENSHIFT_MYSQL_DB_PASSWORD,
port :process.env.OPENSHIFT_MYSQL_DB_PORT,
db :"testnodejs"
});
app.use(express.bodyParser());
connection.connect();
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/users', function(req, res){
connection.query("select * from users where name="+req.query["name"],
function(err, result, fields) {
if (err) throw err;
else {
res.send(result);
}
});
});
app.listen(port,ipaddr);
console.log("Express server listening on port %d in %s mode",port, ipaddr, app.settings.env);