News for hackers. Stuff that matters.
Re:An Absolute Beginner's Guide to Node.js by alanning on Tuesday August 20, 2013 @08:19PM (#6247458)
Thank 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).
Re:An Absolute Beginner's Guide to Node.js by adregan on Wednesday August 21, 2013 @04:50AM (#6248911)
After 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.

Re:An Absolute Beginner's Guide to Node.js by austinrory on Tuesday August 20, 2013 @05:52PM (#6246833)
Great post. Looks like you guys have a cool platform too. Any specifics about how it's better than what's out there?
Re:An Absolute Beginner's Guide to Node.js by zwigby on Tuesday August 20, 2013 @06:23PM (#6246995)
I'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 [email protected].
Re:An Absolute Beginner's Guide to Node.js by mattmanser on Tuesday August 20, 2013 @06:33PM (#6247037)
Ach, go on, a quick elevator pitch won't hurt! I had the same question.
Re:An Absolute Beginner's Guide to Node.js by zwigby on Tuesday August 20, 2013 @08:37PM (#6247507)
Well Modulus, 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.
Re:An Absolute Beginner's Guide to Node.js by Smirnoff on Wednesday August 21, 2013 @04:23AM (#6248830)
Clicking on the modulus logo takes me to your blog site instead of your main site
Re:An Absolute Beginner's Guide to Node.js by austinrory on Tuesday August 20, 2013 @07:04PM (#6247170)
Re:An Absolute Beginner's Guide to Node.js by js2 on Thursday August 22, 2013 @02:05PM (#6258831)
I 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
Re:An Absolute Beginner's Guide to Node.js by gutsy on Wednesday August 21, 2013 @09:07AM (#6249915)
I 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.
Re:An Absolute Beginner's Guide to Node.js by kenshiro_o on Tuesday August 20, 2013 @06:07PM (#6246914)
Interesting article. You guys should maybe split this "cookbook" into multiple articles where you will briefly cover socket.io, Mongoose, and unit testing.
Re:An Absolute Beginner's Guide to Node.js by S4M on Tuesday August 20, 2013 @05:15PM (#6246633)
> 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?

Re:An Absolute Beginner's Guide to Node.js by zwigby on Tuesday August 20, 2013 @06:27PM (#6247011)
The 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.

Re:An Absolute Beginner's Guide to Node.js by oh_teh_meows on Tuesday August 20, 2013 @07:28PM (#6247276)
So 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?
Re:An Absolute Beginner's Guide to Node.js by thedufer on Wednesday August 21, 2013 @12:41AM (#6248225)
Yep. 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.
Re:An Absolute Beginner's Guide to Node.js by catnaroek on Tuesday August 20, 2013 @08:20PM (#6247461)
Remember the "Node.js is Cancer" troll? It was pretty much about this.
Re:An Absolute Beginner's Guide to Node.js by dsego on Tuesday August 20, 2013 @07:55PM (#6247374)
Basically, yes.
Re:An Absolute Beginner's Guide to Node.js by svachalek on Tuesday August 20, 2013 @05:29PM (#6246706)
No, Javascript has no notion of threads. Rather than true concurrency and a synchronization mechanism, it has callbacks (upon callbacks upon callbacks).
Re:An Absolute Beginner's Guide to Node.js by jbmartin on Tuesday August 20, 2013 @07:02PM (#6247156)
Technically 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/Performan...

Re:An Absolute Beginner's Guide to Node.js by thedufer on Wednesday August 21, 2013 @12:38AM (#6248214)
While that's true, I don't believe it's applicable to node.js, so it's a bit out of scope for this article.
Re:An Absolute Beginner's Guide to Node.js by jbmartin on Wednesday August 21, 2013 @03:31AM (#6248717)
People 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 and https://github.com/cramforce/node-worker.
Re:An Absolute Beginner's Guide to Node.js by thedufer on Wednesday August 21, 2013 @09:28AM (#6250080)
Fascinating. I didn't know that webworkers were available in node.js. I stand corrected, then.
Re:An Absolute Beginner's Guide to Node.js by YcombRegBroken on Tuesday August 20, 2013 @06:13PM (#6246947)
Callbacks all the way down. I blame 42% of my recent gray hair on callbacks.
Re:An Absolute Beginner's Guide to Node.js by anaphor on Wednesday August 21, 2013 @07:07AM (#6249329)
You can create lightweight threads built on delimited continuations in javascript, same as any language that supports thunks reified as functions.
Re:An Absolute Beginner's Guide to Node.js by dsego on Tuesday August 20, 2013 @07:53PM (#6247367)
Nope, it is an event loop.
Re:An Absolute Beginner's Guide to Node.js by Shizka on Wednesday August 21, 2013 @07:07AM (#6249330)
I found this screencast about Node.js yesterday. It seems to be in the same vein: http://nodecasts.net/
Re:An Absolute Beginner's Guide to Node.js by supersaiyan on Tuesday August 20, 2013 @06:46PM (#6247092)
I wish there was a rails-android(or ios) authentication guide, there seems to be almost no information on the matter
Re:An Absolute Beginner's Guide to Node.js by kcbanner on Tuesday August 20, 2013 @07:12PM (#6247209)
This is an article about node.js.
Re:An Absolute Beginner's Guide to Node.js by MrBra on Tuesday August 20, 2013 @07:30PM (#6247285)
Not 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..

Re:An Absolute Beginner's Guide to Node.js by dsego on Tuesday August 20, 2013 @08:15PM (#6247448)
For being such a flawed language, it does get one thing right. That one thing made Ryan Dahl pick javascript and it is the relative absence of blocking IO calls. Instead you use events and callbacks.
Re:An Absolute Beginner's Guide to Node.js by catnaroek on Tuesday August 20, 2013 @08:25PM (#6247475)
> "relative absence of blocking IO calls."

Do not confuse "language" with "standard library".

Re:An Absolute Beginner's Guide to Node.js by thedufer on Wednesday August 21, 2013 @12:45AM (#6248237)
The whole event loop idea, which is core to javascript, allows for the single-threaded non-blocking I/O in a way that no other popular language does. Because of this, javascript has features that make this type of programming easy - first-class functions, anonymous functions, etc.

So, yes. Non-blocking I/O is a standard library feature, but it works because of language features.

Re:An Absolute Beginner's Guide to Node.js by catnaroek on Wednesday August 21, 2013 @09:54AM (#6250249)
> "The whole event loop idea, which is core to javascript, allows for the single-threaded non-blocking I/O (...)"

Event loops are not a property of JavaScript of any other programming language: Every single GUI Win32 application ever written in C directly calling the WinAPI contains an explicit event loop.

> "(...) in a way that no other popular language does. Because of this, javascript has features that make this type of programming easy - first-class functions, anonymous functions, etc."

JavaScript is hardly the only language, or even the only popular language, to ever have first-class procedures or anonymous procedures. You know what (at some point in time) widely taught language has a procedure data type[1]? Pascal. So why not Node.pas instead?

> "So, yes. Non-blocking I/O is a standard library feature, but it works because of language features."

That countless other languages have. Most of them a lot better designer than JavaScript.

===

[1] Edit: I originally suggested Pascal has anonymous procedures, which it does not.

Re:An Absolute Beginner's Guide to Node.js by jussij on Wednesday August 21, 2013 @02:30AM (#6248532)
> allows for the single-threaded non-blocking I/O in a way that no other popular language does.

C# Async and Await - http://msdn.microsoft.com/en-us/library/vstudio/hh191443.asp...

Re:An Absolute Beginner's Guide to Node.js by hackerboos on Wednesday August 21, 2013 @04:25AM (#6248841)
Twisted (Python) and EventMachine (Ruby) don't all of these do the same thing?
Re:An Absolute Beginner's Guide to Node.js by thedufer on Wednesday August 21, 2013 @09:27AM (#6250072)
Yes. But they are tacked on to the language after the fact rather than built in from the beginning. My experience is that both language and libraries support that kind of pattern better in node than in Python (no Ruby experience with async).
Re:An Absolute Beginner's Guide to Node.js by dsego on Wednesday August 21, 2013 @06:40AM (#6249234)
I believe twisted was an inspiration for node.
Re:An Absolute Beginner's Guide to Node.js by dsego on Wednesday August 21, 2013 @06:39AM (#6249232)
were those in the language before or after node came out?
Re:An Absolute Beginner's Guide to Node.js by bitwalker on Wednesday August 21, 2013 @07:50AM (#6249507)
async/await are just sugar over the much older asynchronous delegate pattern that has been in the language since day one. So yes, they were.
Re:An Absolute Beginner's Guide to Node.js by dsego on Wednesday August 21, 2013 @08:04AM (#6249568)
are they widely used in APIs to e.g. send a query to the sql database or open a file?
Re:An Absolute Beginner's Guide to Node.js by jussij on Wednesday August 21, 2013 @10:22PM (#6254725)
Yes.

In .Net 4.5 there are Async versions of the web, file, stream, image and SQL category of functions.

It's also making its way into ASP.Net layer.

Re:An Absolute Beginner's Guide to Node.js by dsego on Tuesday August 20, 2013 @08:39PM (#6247516)
Well, the browser implementation I guess. The point being the correct mindset.
Re:An Absolute Beginner's Guide to Node.js by catnaroek on Tuesday August 20, 2013 @09:02PM (#6247571)
"Mindset" is not a property of programming languages. "Semantics" is. And JavaScript's semantics is, by any reasonable assessment, a huge pile of crap.
Re:An Absolute Beginner's Guide to Node.js by DrJokepu on Tuesday August 20, 2013 @09:15PM (#6247614)
Meh, whatever. I can’t believe people still have flamewars about programming languages on the Internet like it was still 1996.
Re:An Absolute Beginner's Guide to Node.js by catnaroek on Tuesday August 20, 2013 @10:07PM (#6247752)
I was not trying to ignite a flamewar. I was just correcting the mistaken idea that JavaScript's "relative absence of blocking IO calls" is somehow a property of the language itself.

In any case, the real issue at stake is whether there is anything about Node.js that makes it particularly good for writing server-side applications. As far as I can tell, the only thing Node.js brings to the table is an event loop. Event loops are not a particularly earth-shattering concept: Anyone who has written a graphical Win32 application by directly using the WinAPI has written an event loop (message loop in Win32 parlance). I do appreciate not having to write the loop myself, but:

1. An event loop can only handle concurrent I/O. Furthermore, because the event loop is hardcoded into every single Node.js instance, there are two legitimate choices that are nevertheless unavailable to a Node.js programmer: a) disabling the loop, if he does not need it, b) spawning multiple event loops as separate threads within a single Node.js instance.

2. Node.js offers no support whatsoever for concurrent computation. I know, I know, all your Web application does is move data between a database and a client. But, if the only use case of Node.js is going to be making Web applications, would it not have been better to make a Web application framework, and provide it as a library?

Languages that actually provide concurrency support (Erlang, Haskell and Rust, to name a few) make writing event loops as easy as it used to be in the 1990s. On top of that, they can be used to write server-side applications that scale.

Re:An Absolute Beginner's Guide to Node.js by dsego on Tuesday August 20, 2013 @10:59PM (#6247913)
Node was created to make scripting fast servers easy. Nothing more, nothing less. Because JS (as implemented in the browser and known by many programmers) didn't carry the baggage of blocking IO calls, Ryan thought it would be a good language for an event-loop based server.

History of Node.js: http://www.youtube.com/watch?v=SAc0vQCC6UQ

Re:An Absolute Beginner's Guide to Node.js by catnaroek on Tuesday August 20, 2013 @11:57PM (#6248071)
Fast Web servers. There is absolutely nothing there that I could use to make, say, a fast database server.
Re:An Absolute Beginner's Guide to Node.js by dsego on Wednesday August 21, 2013 @07:15AM (#6249348)
yes, fast web servers.
Re:An Absolute Beginner's Guide to Node.js by sirkneeland on Tuesday August 20, 2013 @09:46PM (#6247695)
JavaScript is the most widespread language that doesn't lock you down into any particular vendor's platform.

The last thing we need is for everyone to be writing iOS and Android apps and calling it a day.

Come up with a better language that runs anywhere, instantly, so I don't have to give Apple a cent or Google an iota of data on me, and we'll talk!

Re:An Absolute Beginner's Guide to Node.js by morkbot on Wednesday August 21, 2013 @03:19AM (#6248678)
Try Typekit, it makes JS much, much better and it more or less is what JS will become in the future (it follows latest ECMAScript proposals)
Re:An Absolute Beginner's Guide to Node.js by MrBra on Thursday August 22, 2013 @05:27AM (#6256098)
Did you mean Typescript?
Re:An Absolute Beginner's Guide to Node.js by tieTYT on Tuesday August 20, 2013 @07:48PM (#6247351)
In what way is it flawed?
Re:An Absolute Beginner's Guide to Node.js by cgcardona on Tuesday August 20, 2013 @08:04PM (#6247408)
[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

Re:An Absolute Beginner's Guide to Node.js by catnaroek on Tuesday August 20, 2013 @09:11PM (#6247597)
Yes, I am so trapped in my Haskell blub that I cannot possibly understand why JavaScript is so amazing.
Re:An Absolute Beginner's Guide to Node.js by waldohatesyou on Tuesday August 20, 2013 @09:59PM (#6247729)
Have you tried livescript(http://livescript.net/) or clojurescript(https://github.com/emezeske/clojurescript)? I know that they don't have all of Haskell's features but they do bring Javascript a little bit closer to languages like Haskell.
Re:An Absolute Beginner's Guide to Node.js by cgag on Wednesday August 21, 2013 @05:13PM (#6253351)
This is a good example of the ways clojurescript improves on javascript: http://himera.herokuapp.com/synonym.html
Re:An Absolute Beginner's Guide to Node.js by catnaroek on Wednesday August 21, 2013 @05:09PM (#6253335)
I do not particularly care about having a pretty syntax, what I want is the high assurance that the program will not be wrong.
Re:An Absolute Beginner's Guide to Node.js by cgag on Wednesday August 21, 2013 @08:01PM (#6254140)
Have you tried using haste or fay? (I haven't, just curious)
Re:An Absolute Beginner's Guide to Node.js by dsego on Tuesday August 20, 2013 @10:00PM (#6247735)
Who said it was amazing?
Re:An Absolute Beginner's Guide to Node.js by MrBra on Wednesday August 21, 2013 @04:18PM (#6253058)
Thanks for taking the time to make these interesting points.

Nonetheless reading them it just made javascript even more repellent to me.

It looks like a cup of bad tea to which you need to add some corrections and addons in the first place to taste at least as decent as the other teas. But still it's generally preferred to much better alternatives because it comes with this kind of unique (not true anymore) feature of non-blocking IO.

What some programmers don't realize in this, is that this inducted preference is just the result larger and larger scale profits needs, influencing the programming world.

If a competitor enterprise which operates in the same market as yours can answer to 10000 more concurrent visits than you do, then you are out of the market. Or: Internet marketing rules will tell you that if your visitors are not engaged in your website in about 5 seconds they'll leave.

This is why we are now forced to learn javascript.

And... programmatically speaking, it's really sad.

Re:An Absolute Beginner's Guide to Node.js by yareally on Tuesday August 20, 2013 @08:13PM (#6247438)
Typescript[1] remedies a lot of things people dislike about Javascript, including the lack of strict typing. I've been using it with Node lately and it has made me do a 180 towards my interest in making Javascript a sever side language for projects.

[1] http://www.typescriptlang.org/

Re:An Absolute Beginner's Guide to Node.js by tieTYT on Wednesday August 21, 2013 @03:03PM (#6252589)
Nice answer. I've gone from Java->Haskell->Clojure->CoffeeScript. I was never fluent in Haskell->Clojure but I could get around.

But being completely familiar with Java, CoffeeScript/JavaScript feels nice. I really like underscore.js. When googling for why people don't like JavaScript, it seems that CoffeeScript has already fixed all those problems.

Re:An Absolute Beginner's Guide to Node.js by catnaroek on Wednesday August 21, 2013 @05:23PM (#6253389)
So people only care about syntax? No wonder there is so much buggy software out there.
Re:An Absolute Beginner's Guide to Node.js by tieTYT on Wednesday August 21, 2013 @08:36PM (#6254311)
I can only guess you're reacting to me saying, "I like JavaScript because it lets me type less". Unfortunately, I didn't say that.
Re:An Absolute Beginner's Guide to Node.js by kyllo on Wednesday August 21, 2013 @12:04PM (#6251253)
To add to your list of common grievances about JS, there is no integer nor bigint datatype, only doubles, so floating-point rounding errors are common and numbers have an upper limit (2^53). Also, the way variable scoping works in the language, is completely insane.
Re:An Absolute Beginner's Guide to Node.js by MrBra on Tuesday August 20, 2013 @07:55PM (#6247375)