Accessing a MySQL database from Node.JS
devthought.com
devthought.com
The node.js documentation also illustrates an example of this when doing sys calls:
posix.rename("/tmp/hello", "/tmp/world").addCallback(function () {
posix.stat("/tmp/world").addCallback(function (stats) {
sys.puts("stats: " + JSON.stringify(stats));
});
});
vs. posix.rename("/tmp/hello", "/tmp/world").wait();
var stats = posix.stat("/tmp/world").wait();
sys.puts("stats: " + JSON.stringify(stats));
As I understand it the event loop in node.js does not get blocked by calling wait().For example, suppose you have an email application and on the page there's an area with contact information. You could fire off the SQL query to get the contact database and have the callback populate the contact information box on screen when it's ready.
The rest of your program just carries on doing whatever it needs to do: which may be nothing... the entire program can be event driven and waiting for events to happen (like the user clicking somewhere).
This style of programming takes a little getting used to, but you don't need to have some linear piece of code going around checking to see what needs to happen next.
If in the example of contact information you need something else to happen once the box is populated then I imagine you'd fire some event that tells your program what to do.
This said, event driven programming is quite natural when doing network or GUI programming and most good network and GUI frameworks are event driven. Most database libraries or system libraries aren't.
(group)
('a', db.query('SELECT A ...'))
('b', db.query('SELECT B ...'))
('c', db.query('SELECT C ...'))
.addCallback(function(results) {
p(results) // {a: ..., b: ..., c: ...}
});
This will make it very easy to run multiple things in parallel, yet handle all of them in one callback.Check http://github.com/felixge/node-deferred for more info.
Do not use wait() too much, it will be memory expensive.
[1] http://twistedmatrix.com/documents/current/core/howto/defer....
You could get around the lack of naturalness by using something like MethodChain [1] to allow you to write code like this:
posix.rename("/tmp/hello", "/tmp/world")
.stat("/tmp/world")
._(function() {
sys.puts("stats: " + JSON.stringify(this));
});
http://jsclass.jcoglan.com/methodchain.htmlThere was recently committed writeSync, statSync, etc. that perform POSIX API calls synchronously.
There are good reasons that the classic website setup has the database on a different machine than the webserver firewalled in such a way that the database cannot talk directly to machines on the internet. As time goes by and attackers improve in sophistication, those reasons are becoming better, not worse.
This type of contrast is good for entertainment (movies, video games etc) not code samples. Even if you have to go with a dark background and light text make sure it is not extreme. A good sample of a dark site that is contrastually(I made that up) readable is : http://www.smashlab.com/ OR add a 'high contrast' button that you can click such as on: http://www.merixstudio.com/
Trust me when you get older than 27 you'll thank me.