5 ms·
An Absolute Beginner's Guide to Node.js
- S4M 13y ago> Basically you're telling it to do something and when it's done it will call your function (callback). This is because Node is single-threaded. Do you mean multi-threaded?
- svachalek 13y agoNo, Javascript has no notion of threads. Rather than true concurrency and a synchronization mechanism, it has callbacks (upon callbacks upon callbacks).
- YcombRegBroken 13y agoCallbacks all the way down. I blame 42% of my recent gray hair on callbacks.
- jbmartin 13y agoTechnically speaking, HTML 5 introduced web-workers which allows javascript to have multiple threads (see 1). However, it's still useful to think about them as being single threaded since workers are sandboxed, meaning they don't have access to the global namespace. This avoids issues commonly found in concurrent programming. E.g., separate threads cannot read and write to the same variable since they don't have access to each other's namespaces. [1] https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers https://developer.mozilla.org/en-US/docs/Web/Guide/Performan...
- thedufer 13y agoWhile that's true, I don't believe it's applicable to node.js, so it's a bit out of scope for this article.
- jbmartin 13y agoPeople coming from multi-threaded server environments should be aware that multi-threading does in fact exist in Node (scope of parent comment), but it's rather different from other more common concurrent event handling schemes. I.e., you cannot have shared mutable variables across threads (scope of article and ongoing async discussion). For examples, see https://npmjs.org/package/webworker-threads https://npmjs.org/package/webworker-threads and https://github.com/cramforce/node-worker https://github.com/cramforce/node-worker.
- thedufer 13y agoFascinating. I didn't know that webworkers were available in node.js. I stand corrected, then.
- anaphor 13y agoYou can create lightweight threads built on delimited continuations in javascript, same as any language that supports thunks reified as functions.
- deleted 13y ago[deleted]
- zwigby 13y agoThe main application runs in a single thread and then I/O activities happen in an event loop running with a small thread pool. Once one of those activities completes the event loop will give a signal to the main thread of the Node app and the callback will be executed. Not sure if that helped or just confused everyone.
- oh_teh_meows 13y agoSo basically if you have an infinite loop in your app's one and only thread, none of the callbacks will happen because they have to run on that one thread?
- dsego 13y agoBasically, yes.
- catnaroek 13y agoRemember the "Node.js is Cancer" troll? It was pretty much about this.
- thedufer 13y agoYep. Similar to how an infinite loop in the browser will freeze up the page - the browser only gets to control to draw by putting a draw function in the event loop, so if you hog the thread, the page can't update.
- dsego 13y agoNope, it is an event loop.
- austinrory 13y agoGreat post. Looks like you guys have a cool platform too. Any specifics about how it's better than what's out there?
- zwigby 13y agoI'd love to chat about the platform. I'm not sure if this is the correct forum for pimping it. I just don's to turn this comment area into a discussion of the platform. Hit us up on twitter @OnModulus or send an email to feedback@modulus.io.
- mattmanser 13y agoAch, go on, a quick elevator pitch won't hurt! I had the same question.
- zwigby 13y agoWell Modulus, http://modulus.io http://modulus.io, has a number of things that set us apart. One is that we're not really into to tiers. Custom SSL, domains, etc... are part of the normal package. We support WebSockets, there are a couple others that do but most don't. The really interesting piece is that we provide our customers with a great set of statistics about your running application (number of requests, response times, cpu, memory and more). We also have MongoDB hosting in house which lets you spin up a database for your application quickly.
- austinrory 13y agoso modest http://www.enjoy-your-style.com/images/amish-women-swimwear.jpg http://www.enjoy-your-style.com/images/amish-women-swimwear....
- Smirnoff 13y agoClicking on the modulus logo takes me to your blog site instead of your main site
- kenshiro_o 13y agoInteresting article. You guys should maybe split this "cookbook" into multiple articles where you will briefly cover socket.io, Mongoose, and unit testing.
- supersaiyan 13y agoI wish there was a rails-android(or ios) authentication guide, there seems to be almost no information on the matter
- kcbanner 13y agoThis is an article about node.js.
- MrBra 13y agoNot trying to be flamy, but, once again... why people insist on pushing on projects based on a... renownedly flawed programming language? ..Yea JS. Today it's clear that the "only" reason for it being used is its widespread availability in browser, but that started in a time when you needed a way to change the mouse pointer from default to a rainbow colored one. But still js it's the programming language we build frameworks for today, and the kind of industry standard we are proud of.. today. We are we still stuck at this point? [rhetorical question] I am not even close to being capable of fixing this by myself so obviously the reason for this comment is just to create my own little tiny wave of resonance, hoping that it could hurt the next bigger wave, hoping this could go on forever.. till the point that js is replaced by something better and wiped forever from planet earth..
- tieTYT 13y agoIn what way is it flawed?
- MrBra 13y agohttp://lmgtfy.com/?q=javascript+design+flaws http://lmgtfy.com/?q=javascript+design+flaws
- cgcardona 13y ago[edit spelling] I think that the reason you find so much negativity regarding javascript is that many people come to javascript from some other language which offers features which javascript doesn't yet offer. Having cut their teeth on a language tends to leave the programmer with a worldview where any language which doesn't have the same features which are present in their beloved language is an inferior language. I believe Paul Graham refers to this as 'The Blub Paradox'[0]. Most of the complaints surrounding javascript stem from the fact that it was initially designed to be a lightweight scripting language. At the time that javascript was created I think that very few people envisioned the web growing into what it has become. Javascript wasn't created with 100,000 line programs in mind. Therefore it is currently missing some abstractions which people creating and maintaining programs in the large have come to rely on such as classes, interfaces and modules. Also javascript has a very simple standard library which leads to people feeling that they need to reinvent the wheel in most cases. Finally Javascript has prototypal inheritance as opposed to the more traditional class based inheritance which so many programmers are used to. Of course many of the things mentioned above have already been solved or are currently being solved. The ECMAScript standards body is in the process of adding classes and modules. Libraries such as underscore.js are fleshing out the standard library. And tools such as Coffeescript are providing a more class based inheritance model (if that is your thing). [0] http://www.paulgraham.com/avg.html http://www.paulgraham.com/avg.html
- alanning 13y agoThank you for writing this. Keeping the target audience in mind, one thing you may want to consider is telling people how to stop the node server once its started (CTRL-C).
- adregan 13y agoAfter teaching university, and now having spent the past few years teaching teens it's best not to take for granted what a student might know. Though I discovered that on all levels (not just by teaching the teenagers). When you say "absolute beginner", you've got to be careful not to make too many leaps. The advice above regarding CTRL-C is good. An absolute beginner might not know this. Also I noticed a leap in the "static file server" lesson. A beginner may not realize they have to create a public directory and will just get a very unfriendly "Cannot GET" error. This might be especially true as following the `npm install express` command, directories were generated automatically. This sets up an expectation that isn't fulfilled. A little more handholding might keep you from answering as many questions and allow the beginner a really smooth entry.
- Shizka 13y agoI found this screencast about Node.js yesterday. It seems to be in the same vein: http://nodecasts.net/ http://nodecasts.net/
- gutsy 13y agoI like this article. I've been learning Node on the side (I'm a Java developer so Node is pretty foreign to me) and really enjoying it, but as it's very different from what I'm used to these kinds of guides are a big help.
- js2 13y agoI use Node.js a lot for scripting like this. It's much easier and a more powerful alternative to bash scripts. I'll use node for Unix scripting when you pry every other alternative from my cold dead hands: awk '{a[$2] += $3};END{for(x in a){print x, a[x]}}' sample.txt A 2 B 14 C 6