Created
July 7, 2011 10:27
-
-
Save schamane/1069260 to your computer and use it in GitHub Desktop.
example for mysql client http://forum.nodejs.ru/index.php/topic,623.msg4049.html
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 Controller = function(req, res) { | |
var DB = require("./db").DB; | |
this._db = new DB(); | |
this._req = req; | |
this._res = res; | |
console.log(new Date() + ' : ' + req.url); | |
}; | |
Controller.HTTP_OK_CODE = 200; | |
Controller.HEADERS = { | |
'Content-Type': "text/plain", | |
'charset':"utf-8" | |
}; | |
Controller.prototype.sendCategories = function() { | |
this._db.connect(function(){ | |
this._db.setDBForUTF8(function(){ | |
this._selectCategories(this._send.bind(this)); | |
}.bind(this); | |
}.bind(this)); | |
}; | |
Controller.prototype._send = function(err, results, fields) { | |
if (err) { | |
//throw err; | |
res.end(); | |
client.end(); | |
return; | |
} | |
res.writeHead(Controller.HTTP_OK_CODE, Controller.HEADERS); | |
res.end(JSON.stringify(results)); | |
client.end(); | |
delete this._db; | |
}; | |
Controller.prototype._selectCategories = function(cb) { | |
var url = require("url"), | |
res = this._res, | |
parentId = url.parse(this._req.url).pathname.split('/')[2]; | |
parentId = parentId == undefined?0:Number(parentId); | |
var q = 'SELECT cat_id, cat_name FROM 4im_categories WHERE cat_parent_id = '+parentId; | |
this._db.client.query(q,cb); | |
}; | |
exports.Controller = Controller; |
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 Db = function() { | |
var Client = require('mysql').Client; | |
this.client = new Client(); | |
} | |
Db.PASSWORD = "YYYYYY"; | |
Db.USER = "XXXXX"; | |
Db.DB = "my_db"; | |
Db.prototype.connect = function(cb) { | |
var client = this.client; | |
client.user = Db.USER; | |
client.password = Db.PASSWORD; | |
this.afterConnect = cb; | |
client.connect(this.setDB.bind(this)); | |
}; | |
Db.prototype.setDb = function(error, result){ | |
if(error) { | |
console.log(error); | |
return; | |
} | |
this.client.query("USE "+ Db.DB, function( error, result) { | |
if(error){ | |
console.log(error); | |
return; | |
} | |
this.afterConnect(); | |
}.bind(this)); | |
}; | |
Db.prototype.setDBForUTF8 = function(cb) { | |
this.client.query("SET NAMES utf8", function(error, result){ | |
if(error) { | |
console.log(error); | |
return; | |
} | |
cb(); | |
}); | |
}; | |
exports.DB = Db; |
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 http = require('http'); | |
var url = require("url"); | |
http.createServer( | |
function (req, res){ | |
var modname = url.parse(req.url).pathname.split('/')[1]; | |
try{ | |
var mod = require("./"+modname); | |
var controller = new mod.Controller(req, res); | |
controller.sendCategories(); | |
}catch(e) | |
{ | |
console.log(new Date() + ' : ' + e); | |
} | |
} | |
).listen(1337, "127.0.0.1"); | |
console.log('Server running at http://127.0.0.1:1337/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment