用express写的一个node脚本:
var express = require('express');
var app = express();
app.get('/shorten', function (req, res) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '121.42.209.162',
user : 'root',
password : 'password',
database : 'mydatabase'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
var long=req.query.long;
console.log(long);
connection.query("SELECT * FROM url WHERE long='"+long+"'", function(err, rows) {
if(err){
console.log(err.code); // 'ECONNREFUSED'
console.log(err.fatal); // true
res.send("fail");
}else{
res.send("ok");
}
});
connection.end();
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
运行起来了以后,用浏览器打开http://localhost:3000/shorten?long=test
浏览器里显示的是fail
服务器日志里面报错
long是数据库保留字吧, 设字段的时候搞个前缀试试