What is Node.JS and Why Should I Care? [Web Development]


JavaScript is a just a client-side programming language that runs in the browser, right? Not any more. Node.js is a way of running JavaScript on the server; but it’s so much more as well. If you’re at all interested in web development, you really should find out a little about Node and why it’s making waves in the community.

What is Node.js?

Node is an interface to the V8 JavaScript runtime – the super-fast JavaScript interpreter that runs in the Chrome browser. As it happens, you can also download V8 and embed it into anything; Node does that, for web servers. JavaScript is after all, just a language – there’s nothing that says it couldn’t be used on a server as well as in the user’s browser. In a typical LAMP server stack, you have an underlying Apache or NGINX web server, with PHP running on top of it. Each new connection to the server spawns a new thread, and it’s very easy to quickly lose performance or for a site to “go down” – the only way to support more users being to add more servers. It simply doesn’t scale well. With Node, this isn’t the case. There is no Apache to listen for incoming connections and return HTTP status codes – you’ll need to handle that core server architecture yourself. Luckily, there’s modules to make this easier, but it can still be a little overwhelming when you start out. The result, however, is a high performance web application.
Klout - a social statistics web app built upon Node.js for high performance
KLOUT – A SOCIAL STATISTICS WEB APP BUILT UPON NODE.JS FOR HIGH PERFORMANCE
JavaScript is an event-based language, so anything that happens on the server triggers a non-blocking event. Each new connection fires an event; data being received from an upload form fires a data-received event; requesting data from the database fires an event. In practice, this means a Node site will never lock up and can support tens of thousands of concurrent users. Node.js plays the role of the server – Apache – and interprets the application code being run on top of it. Just like Apache, there are various modules (libraries) that can installed to add features and functionality – like data stores, Zip file support, Facebook login, or payment gateways. Of course, there aren’t nearly as many as for PHP, but Node is still in it’s early stages and there’s a strong community behind it. A core concept of Node is asyncronous functions – so everything runs in the background, basically. With most server side scripting languages, the program has to wait whilst each function completes before going on to the next. With Node, you specify functions that should be run on completion of something else, while the rest of your app moves on. It’s a complex topic that I won’t go into too much depth today, but one of those characteristics that defines Node, so it’s important to grasp it. Enough chat though – here’s a Hello World example HTTP server to give a quick idea of some of these concepts.

var http = require("http"); 
http.createServer(function (request, response) {
 response.writeHead(200, {
 'Content-Type': 'text/plain'
 });
 response.write('Hello World!');
 response.end();
}).listen(8080);
console.log("Server running!");
Let’s try to break that down. First, we’re including the http module in the project. We’re then creating a server and passing in an anonymous function as a parameter – this function will be called for every new connection that’s made. It takes two arguments – request, containing the request parameters from the user; andresponse, which we’ll use to send things back. Next, we’re using the response object to write a header back to the user with the response HTTP code 200 (“ok”)and content type, writing a “Hello World!” message, and ending the response. Finally, we tell the server to listen on port 8080 for incoming requests, and output a quick message to the console (the command line) to let us know it’s running. If you want to actually try this for yourself, save the above code as test.jsdownload the Node installer, and from the command line run –
node test.js
Open up a browser and navigate to localhost:8080 to see your test app! You should now have a little idea of how this whole asynchronous thing works, along with event driven models. If you’re new to JavaScript, the concept of passing around functions as arguments to other functions is probably a little strange.

Why use Node?

Firstly, for performance and scalability. Node is fast. That’s a pretty important requirement when you’re a start-up trying to make the next big thing and want to make sure you can scale quickly, coping with an influx of users as your site grows. Node is also perfect for offering a RESTful API - a web service which takes a few input parameters and passes a little data back – simple data manipulation without a huge amount of computation. Node can handle thousands of these concurrently where PHP would just collapse. Performance benefits and scalability aside, there’s a good chance you already know some JavaScript, so why bother learning a whole new language like PHP? And then – there’s the excitement of learning something new and relatively uncharted. You know when something new arrives and then becomes so ubiquitous that you regret not learning it earlier, forever playing catchup? Don’t do that this time. Node is going to be big.

Downsides

Like most new technologies, it’s not that easy to deploy Node on existing hosts. If you have a shared web hosting, you can’t simply upload a Node app and expect it to work. VPS and dedicated servers are better positioned – you can install Node on them. Even easier is to use a scalable service like Heroku, which is completely free to develop your site on – you only need to pay when you need more resources. I gave an example of using Heroku before when we used it to create a Facebook fangate, but it can be used for Node too. On the other hand, it’s very easy to install Node locally onto your Windows, Mac or Linux PC and begin developing immediately – just head over to Download Node. It’s also important to note that Node is not simply a replacement for Apache – existing web applications are not compatible, and you’ll be working effectively from scratch (though there are a lot of frameworks out there to help you with common features).  The other major downside to node is that it’s still in the early stages of development, meaning some features are likely to change as development progresses. In fact, if you look at thedocumentation, it includes a stability index, which shows how risky use of each feature is currently.stabilityYou know – it’s never been a more exciting time to be a web developer. With open web services and data exchange, it’s easier than ever to make somethingawesome. Are you think of learning Node? Good. Go forth and make the next Twitter! You’ll probably also want a good grounding of the Javascript jQuery libraryfor the front end.

Commentaires