Ask HN: Why Node.js and for which type of App?
Hi guys,
i'm a computer scientist student and previously read a lot about Node.js. But, to be honest, i have not really a feeling for what i can use a node.js App. Is it a desktop application where i can use html to render the views and the communication between view an backend works i.e. with a RestAPI? Or is it for pure web applications?
Thanks a lot for your inputs.
edit: started yesterday with the node.js tutorial from Visual Studio Code and was just curious what type of app i can build with it
48 comments
[ 1.8 ms ] story [ 93.8 ms ] threadWhat you are probably looking for is Node-Webkit. It is a toolkit to basically pack a stand alone application with a browser and node.js to create a desktop application.
I personally would recommend to use QT or GTK or some Windows framework if you want to learn how to program a desktop application. No need to deal with the performance problems of web technology and then ship a whole browser with your app if you have to learn something new anyway. In my opinion you should use Node-Webkit only if you know why you use it or if you do not know how to use anything else.
I programmed some Desktop Apps with Java and C#. And during some hackathons i used python+flask+nginx to implement a Web-Applications with a RestAPI on the backend.
I was just curious for which type of application i can use node.js (since i didn't use javascript on the backend at all). Usually i used PHP, C# or Pyhton for the backend..
Is MS Windows 10 using node.js for their applications (since its possible to develop Node.js apps in Visual Studio Code)?
Windows doesn't use node, they have their own JS runtime and a GUI framework that you can script with JS.
You can also run multiple node processes on the same machine (on multiple ports) and use something like NGinx to loadbalance between them. You can use this to get zero-downtime upgrades on a single machine.
For example, if your app is just getting data from a database and returning it to the user, 90% of your time will be spent:
* Receiving the user's request (Network, IO)
* Fetching something from the DB (Network, IO)
* Returning the data (Network, IO)
That's a good scenario for an event based system like Node.js.
Try not to use node.js if you're CPU-bound: rendering images or PDF, processing sound, etc. If you do need to do something like that and you still want to use node.js you should use a processing queue.
So, you would use node.js just as a service to process data on the server side and not as a application for the desktop (with a gui)?
However, there is growing discussion about using Node.js for desktop applications. Please see https://nodesource.com/blog/node-desktop-applications
Desktop apps are insanely high frequency IO apps, basically, its an Input Queue reading the mouse / keyboard / touch screen and a run loop, which is exactly what node is, except node originally envisioned the input queue being web requests. And most desktop apps are single threaded on the UI.
Since Node / nginx are already processing queues it's kinda pointless to add another one.
There are many languages and environments for running code on the server, so I personally have problems finding a niche where Javascript is the obviously better choice for these tasks. In my experience with it so far, it's "fast enough" for IO bound tasks (routing, serving CRUD applications), but if you'd like to perform any form of computation on inputs, you'd probably be better served by a different language ecosystem.
Now that's often what people think of Node.js, but that's not really why Node.js is interesting. It's not just "JavaScript on the browser".
Instead, you should think of Node.js more like a JavaScript binding to `libuv`, which is the C library that provides the event-driven I/O core (event loop / queue). (An example for a `libuv` binding in another language, Lua would be `luv` [1].)
Reasons why JavaScript was picked for Node.js: 1) It already dealt with events due to its usage in browsers and 2) a fast existing JIT runtime, V8.
To answer the original question: You might want to use Node.js if your problem is a good fit for the Reactor pattern [2]. For example, scalable network services that "mediate" between a lot of (async) I/O. Not number crunching. Nginx uses the same core concept (Reactor).
[1]: https://github.com/luvit/luv
[2]: https://en.wikipedia.org/wiki/Reactor_pattern
> Reasons why JavaScript was picked for Node.js: 1) It already dealt with events due to its usage in browsers and 2) a fast existing JIT runtime, V8.
Well, no, the original creator of Node just wanted non-blocking IO (which the frameworks he was using did not support), and (I'm editorializing a bit here) in the grand engineering tradition of re-inventing the wheel, he decided to use Javascript with v8 to create a whole new platform, instead of using an existing platform.
In my opinion, Node's biggest advantage today is the absolutely massive module ecosystem that has sprung up around it in record time. It's already the biggest, as well as the fastest growing according to http://www.modulecounts.com/ You can find a package for almost everything imaginable, and your typical Node app is made up of dozens of these modules. So take a look at https://www.npmjs.com/, and try searching for whatever field interests you.
Nope, Atom it's based on electron[0] itself which use io.js and Chromium as a core.
[0] https://github.com/atom/electron
http://www.infoq.com/news/2015/05/nodejs-iojs
EDIT: Clarify with "CPU/disk intensive operations"
Making restful APIs is quite fun with it, though.
npm[0], Node.js's package manager, is also pretty useful. There's a package to do most things.
Yes, there's some projects that let you create desktop applications using Node.js - what they're doing is using an embedded version of Chrome and pairing it with Node.js. You can then use HTML and CSS to create a user-interface that you can then manipulate with JavaScript. You end up with a fairly bloated distributable application, but the JavaScript code runs on Mac, Windows and Linux.
NodeSchool[1] seems to be the recommended learning resource for Node.js. I've never really used it, but it looks pretty good.
[0] https://www.npmjs.com/ [1] http://nodeschool.io/
For example, I share the physics code which is responsible for controlling the simulation that runs on both the server (authoritative) and clients (for predictions).
I no longer have to switch languages which also saves a lot of time!
People have also used it for desktop applications, with a few desktop UI library wrappers. Githubs text editor 'Atom' for example.
Node also has a great set of API's for interacting with the system and network utilities, so there's a lot of great tools for networking and automation.
Finally, because Javascript is so lightweight, we're seeing it play a key role in the 'Internet of Things'. Some micro-controllers are even written in JS (see Tessel).
So don't feel as though it's for one thing or another, just have fun with it and explore the language and its potential!
Some of the APIs are not very portable either, such as fs.watch. Many npm libraries implicitly depend on Linux or OS X.
It's convenient though, sure.
People have also used it for desktop applications, with a few desktop UI library wrappers. Githubs text editor 'Atom' for example.
Node also has a great set of API's for interacting with the system and network utilities, so there's a lot of great tools for networking and automation.
Finally, because Javascript is so lightweight, we're seeing it play a key role in the 'Internet of Things'. Some micro-controllers are even written in JS (see Tessel).
So don't feel as though it's for one thing or another, just have fun with it and explore the language and its potential!
It is also great when you want to prototype a REST backend, and get it up and running in no time. The simplicity of javascript along with nodejs' module ecosystem make it a breeze (Ruby equivalent would be Sinatra).
What nodejs isn't good at is, parallelism. Concurrency is handled for you for free by the underlying libuv eventing framework. Pseudo-parallelism is achieved using 'clusters' but communication between node processes is costlier than in other languages that support parallelism out of the box.
Read https://news.ycombinator.com/item?id=4305486 for aphyr's excellent commentary on clojure vs nodejs (you can also find Ryan Dahl defending nodejs in a few cases there)
On the other hand it's javascript, so :-)
I am still figuring out a lot of things about Python, and this is something I found very intriguing. What would the differences between an IM app written in Python using Flask and it's nodeJS clone be, to a user?
See also, aiohttp [2] which seems very promising (Python 3.3+). As an aside, I've never seriously considered Python 3 till now after taking a look at aiohttp.
[1] https://flask-socketio.readthedocs.org/en/latest/ [2] http://aiohttp.readthedocs.org/en/stable/web.html
https://github.com/eventmachine/eventmachine/wiki/EM-vs-Twis...
http://community.eveonline.com/news/dev-blogs/stackless-pyth...
Node.js is one of the few webservers built ground up async.
The three most important reasons to use Node.js: it uses JavaScript which you might want to learn already, and second, it has a big ecosystem of modules and frameworks, including by companies like Microsoft and Google, and third, there is more JavaScript code on GitHub than any other language, if you're looking into contributing to open source projects or learning from them.
What you can do with Node.js:
JavaScript was already important as the language of the web browsers. As soon as JavaScript was split off from the web browsers thanks to the Node.js project, people have been writing web servers as well as command-line tools with it. It's also used to create interactive cross-platform native mobile apps and chrome/firefox extensions.
As a bonus, the asynchronous nature of JavaScript and its ubiquity in web browsers as well as web servers has lead to the creation of a wealth of official and unofficial standards and implementations of libraries for creating real-time chat and systems.
If you can do most everything in one language, why not?
Edit: someone already wrote a more technical overview, if you would like one:
https://news.ycombinator.com/item?id=9830207
I left that out because some commenters mentioned that as the leading feature. But for somebody who's asking what Node.js is used for, I don't think being asynchronous is as important a reason as the fact you're writing JavaScript with great tooling, frameworks, and modules.
As far as coding day-to-day, there are other languages that have better semantics around writing asynchronous code. And it rarely makes a difference for first-time developers until they actually build something and try to scale, and at that point would need to read articles about doing that with Node.js just like they would with Ruby/Rails, Clojure, Go, etc.
Gulp and Grunt have been very popular as a way of, for instance, watching files and then minifying CSS an JS assets automatically while creating web pages in systems like WordPress that don't have built-in asset pipelines.
But in my opinion a lot of Node.js ecosystem is a lot of mismarketed features. Many developers doing backend services with Node.js actually think that it's the fastest thing available, even though multiple benchmarks, e.g. techempower, shows that it really isn't. And even more people seem to think that it is a way to do simple parallellism, so they won't have to understand threads and locking, which are really complicated stuff. But as many have said here, Node.js does not support threads, or parallelism without running multiple different processes. Which can be fine if you don't have any shared state between your processes. And with no parallelism in process, it is quite easy to actually block the event loop by running anything that is CPU and not IO bound. This can be a loop that is too long, too much math, or even parsing a JSON string without using streams. All of these can block the event loop, which means that no requests are going through that process while one request is parsin a JSON.
And even though there is a lot of libraries and frameworks for it. The quality is often really, really bad. As in invalid MD5 algorithm bad, etc. But there are also some gems such as Bluebird for promises, which makes the callback hell more easier to handle.
You will also face immature debug support, profiling and static analysis. You barely get any refactoring help from your tools, even though IntelliJ IDEA does quite a good job with basic refactoring and debugging. And you will have to spend time with handling odd bugs with no logs showing up on crash, or stuck processes when something has gone really wrong in the code, with no way of knowing (if you don't have DTrace) where the code is stuck.
But there are stuff Node.js seems to excel at. It is really quick for creating a simple REST service, feedback loop is really quick as the services restart almost immediately (at least when you don't use all the latest ES6 transpilers). And if you want to create isomorphic applications, where the server can render a Javascript site on behalf of the browser for the first request, or even successive requests for mobile use, there is no better platform than Node.js. And if you know that you will not do anything that is CPU bound, just IO bound stuff, you can still use any library available. Where for example in Java or Python, you would have to find specific libraries that support your chosen async IO framework.
I would use Node.js between a backend server done in a more robust ecosystem such as JVM, and the browser. Where Node.js gets the data from the backend and does it's magic with isomorphic React for the client.