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

featured node   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   What is Node.JS and Why Should I Care? [Web Development]

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!');
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; and response, 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.js, download 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.


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 the documentation, it includes a stability index, which shows how risky use of each feature is currently. stability   What is Node.JS and Why Should I Care? [Web Development] You 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 something awesome. 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 library for the front end.

The comments were closed because the article is more than 180 days old.

If you have any questions related to what's mentioned in the article or need help with any computer issue, ask it on MakeUseOf Answers—We and our community will be more than happy to help.


0 votes

Karl J. Gephart

Can you recommend any good, low-cost (yeah, I know!) educational sources to learn Node.JS? I’m studying JS right now (very comfortable with CSS/HTML and jQuery), and have recently decided to put off learning PHP (especially after reading your post) until I learn Node.JS.

0 votes

MakeUseOf TechGuy

I had a trial of https://www.codeschool.com/ previously – and was very impressed. The end of chapter interactive coding sessions really make a difference. I’m not sure what you’d define as low cost, but that series costs $25/month for access to all their courses.

A completely free tutorial can be found at http://www.nodebeginner.org/ – but you’ll want to practice on your own machine with that as there’s no interactive lessons.

0 votes

Chew J

Can you use nodejs to connect to an existing MySQL database? How can I convert my PHP scripts to NodeJS?

0 votes

MakeUseOf TechGuy

You can indeed connect to MySQL: https://github.com/felixge/node-mysql . However, you cannot “convert” PHP scripts. With node, you need to code the webserver too. You could get the same functionality as you have now, but it would be a case of recoding everything from scratch.