Beginning Node.js Development
javascriptplayground.com
javascriptplayground.com
nginx -> Static html/javascript/images/css
node.js -> API
Apache -> old Python CGI stuff I still want (beautiful soup, for instance)
To me, this stack is a lot more comfortable, and is a lot faster.
----
Here are a couple of things that this tut misses.
1) You need to talk to your database. This will become immediately apparent when you try to do anything. You can google around, which will lead you to stack overflow, or you can just listen to me right here and use "mysql".
npm install mysql
Easy.2) Callbacks. This is freaking huge. node will not make any sense to you if you do not understand this. For instance, if you have a function called "get_username()" (Hi! I have a function called get username, I'm speaking from a mistake I made about this!), and you call it like this:
username = get_username();
res.write("Hi, you are: " + username);
res.end("buhbye!");
Your page is just going to crash because username is undefined. This is because node is nonblocking. It doesn't wait for get_username() to return before continuing...meaning your server will end before get_username() finishes.This is a problem.
So what you need to do won't make sense at first (and maybe I'm doing this wrong, correct me if I am).
you would do:
get_username(res);
and then you would have: function get_username(res) {
do_some_things;
db.query(query, function() { ;
//this is your db connectors callback function. It fires when your query is finished.
username = some_query_results['user'];
res.write("Hello, you are: " + username);
call_some_other_function();
or_if_this_is_the_last_thing_you_want_to_do_call res.end();
});
}
The point here [and this is obviously just pseudocode] is that you don't just write out a list of functions. You write functions that call each other when they're finished.read this: http://blog.mixu.net/2011/02/01/understanding-the-node-js-ev...
You misunderstand how non-blocking works. In this case, it -most definitely- waits for get_username() to return before continuing. If get_username itself does some asynchronous calls (like db.query), that's its problem, but execution absolutely waits for your own functions to finish.
I'd recommend reading up more on event-driven programming.
My get_username() function, say I have this code:
var server = http.createServer(function(req, res) {
username = get_username();
res.writeHead(200);
res.write("<html>");
res.write("You are " + username);
res.end("bye");
This obviously won't work, res.end() gets fired long before my DB query in get_username() finishes. How else would I accomplish what I'm trying to do here other than putting my response stuff in the DB query's callback function?<Sorry for the newb question, buy what I'm doing: having the response generated inside of the query's callback, seems to be the way most of these node tutorials suggest.>
In practice, you can find a ton of libraries that can help you manage this [1], I -believe- the most recommended one would be 'futures'.
[1] https://github.com/joyent/node/wiki/modules#wiki-async-flow
I can't seem to find any good design/architecture for node.js articles though, does anyone have any links?
I recommend that folks new to Node check out some much more in-depth tutorials that actually explain callbacks and event-driven design. Google for "Mixu's node book" and "The Node Beginner Book" for a more in-depth introduction to Node.