How to Node: Server-side JavaScript at Barcamp Bangalore

Node.jsJavaScriptServer-sideWeb Development

An introduction to Node.js, exploring its capabilities, best use cases, and why it gained so much attention in the web development world.

How to Node: Server-side JavaScript

Note: This is a restored article from the original A thousand nodes blog (circa 2011). It combines content from presentations I gave at Barcamp Bangalore in June 2011.

Introduction to Node.js

What is Node.js? Why is it creating so much buzz? It should have been simple as it's just JavaScript running on the server. But it's not meant for everyone.

In this article, based on my Barcamp Bangalore presentation, I'll explore how Node.js works, the problems it can easily solve, and when you should (or shouldn't) use it.

What is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

In simpler terms, it allows you to run JavaScript on the server side, outside of a browser environment.

Key Features

Event-Driven Programming

Node.js is built around callbacks - functions that are called when tasks complete. This allows for non-blocking operations, making it efficient when handling multiple connections simultaneously.

const http = require('http');

// Create an HTTP server
const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

// Listen on port 3000
server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Single-Threaded but Highly Scalable

Despite being single-threaded, Node.js can handle a large number of concurrent connections through its event loop mechanism:

// This code can handle thousands of concurrent connections
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
    
    // Broadcast to all clients
    wss.clients.forEach(function each(client) {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });
});

Best Use Cases for Node.js

Node.js particularly shines in these scenarios:

  1. Real-time Applications: Chat applications, gaming servers, or collaborative tools
  2. API Servers: RESTful services that connect to databases
  3. Streaming Applications: Video or data streaming services
  4. Single Page Applications: Backend for JavaScript-heavy frontends
  5. Microservices: Small, focused services in distributed architectures

When Not to Use Node.js

Node.js isn't suitable for:

  • CPU-intensive operations (like heavy computation or image processing)
  • Applications requiring blocking operations
  • Systems where thread-per-request models are more appropriate

Cross-Browser Elements Demo

During my presentation, I showcased a proof of concept using Node.js and Socket.IO that demonstrated the capabilities of asynchronous socket-based websites. The demo simulated elements being moved around in different browsers, effectively showing different clients connected to the same server.

This simple demo illustrated how real-time, bidirectional communication enables new kinds of interactive web experiences that weren't easily possible with traditional web technologies.

Getting Started with Node.js

To begin using Node.js, you'll need to:

  1. Install Node.js: Download from nodejs.org
  2. Create a simple application:
// Save as app.js
const http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000);

console.log('Server running at http://localhost:3000/');
  1. Run your application:
node app.js

Conclusion

Node.js introduced a new paradigm in server-side development by bringing JavaScript to the server. Its event-driven, non-blocking I/O model makes it an excellent choice for applications that need to handle many concurrent connections with low latency.

While not suitable for every type of application, Node.js has found its place in the web development ecosystem, particularly for real-time applications and APIs.

If you're interested in exploring Node.js further, check out the official documentation and the numerous packages available through npm, the Node Package Manager.

Continue Reading

Browse All Articles