4 ms·
Beginning Node.js Development
- danyork 14y agoNice to add the tip about creating a "package.json" file.
- JPKab 14y agoThis is a nice addition to other Node.js tutorials. But a few gripes I have: 1) No explanation of callbacks or event-driven architecture 2) Mac specific instructions only. Why would you limit your tutorial to a specific brand of hardware? Please include Linux instructions or at least a link. Not everyone who wants to code wants to overpay for their hardware. Macs are great, and they are priced as a luxury good, and are consequently completely unaffordable for developers in the non-Western world. 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.
- base698 14y agoNPM now comes with Node out of the box.
- blhack 14y agoFirst: node is freaking amazing. I've been writing python CGI on Apache for the last few years, but can easily see myself moving to: 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-event-loop/ http://blog.mixu.net/2011/02/01/understanding-the-node-js-ev...
- aGHz 14y ago> This is because node is nonblocking. It doesn't wait for get_username() to return before continuing. 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.
- blhack 14y agoGreat, could you point me somewhere to learn more about the topic?
- aGHz 14y agoSorry, nothing off the top of my head, but a good starting point would be googling for stuff like "node event driven" or maybe "event driven programming" in general.
- blhack 14y agoNot sure if you're still monitoring this thread [no HN notifications, eh?], but here's my question: 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.>
- aGHz 14y agoManually, you could structure it in several functions like prepare_response() and render_response(), possibly with other functions in-between for the various chunks of data you need in your response. When all the chunks are ready, you finally call render_response() and send it. 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 https://github.com/joyent/node/wiki/modules#wiki-async-flow
- yskchu 14y agoYet another "hello world" tutorial; does the world really need another "hello world" node.js tutorial? I can't seem to find any good design/architecture for node.js articles though, does anyone have any links?
- whitewhim 14y agoThere are a ton of "hello world" tutorials around. As someone who's currently trying to learn node + express it is very difficult to find tutorials that properly explain everything that are still up to date.