Created
March 4, 2012 20:54
-
-
Save pepoviola/1974743 to your computer and use it in GitHub Desktop.
node.js and mysql module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Client = require('mysql').Client, | |
client = new Client(); | |
client.user = 'user'; | |
client.password = 'password'; | |
client.host='127.0.0.1'; | |
client.port='3306'; | |
client.database='DB' | |
client.connect(); | |
var http = require('http'); | |
http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
client.query("select * from table where campo1 > 1 limit 10;", | |
function select(err, results, fields) { | |
if (err) { | |
console.log("Error: " + err.message); | |
throw err; | |
} | |
console.log("Number of rows: "+results.length); | |
console.log(results); | |
res.write(results); | |
//loop over the result set | |
for (var i in results){ | |
var result = results[i]; | |
res.write(result.campo1+"\n"); | |
} | |
res.end(); | |
}); | |
}).listen(1337, "0.0.0.0"); | |
console.log('Server running '); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment