How do you create server-side Java scripts


Server-side JavaScript with node.js

JavaScript has impressively established itself as a scripting language for web browsers in recent years. However, the language is by no means restricted to client-side applications. Today Ralf Graf presents node.js: a server-side implementation of JavaScript that is ideally suited for demanding multi-user and real-time web applications.

Every web worker knows JavaScript. But what not everyone knows: You can not only use JavaScript as usual in the browser on the client side, but also on the server side to create web applications. This is not necessarily new, e.g. the JS engine Rhino, implemented in Java, has been around for a long time, which is used in the weblog engine and community Antville, which is popular with old-school bloggers.

Server-side JavaScript became really popular with node.js. developed by Ryan Dahl in 2009. This is a JS framework based on the V8 JavaScript engine developed by Google, which is used in Google's Chrome browser. So you can use JS for any server-side tasks. Instead of using JS in the browser and another language such as PHP, Ruby or Python on the server, JS programmers can now limit themselves to JavaScript. Anything that can be implemented with the usual server languages ​​can also be done by node.js.

It can also be used as a command line-based JS interpreter to execute JavaScript outside of a browser. This uses e.g. the popular JS meta language CoffeeScript to compile CoffeeScript code in JavaScript.

An architecture for high performance

But Node.js is not just a "PHP replacement in JavaScript". It was developed in an "event" -controlled architecture and is therefore characterized by high performance that can only be achieved with difficulty with PHP and Co. with the same hardware. This is particularly evident in fashionable, frequently updated "real-time web" applications.

What does that mean? Usually web frameworks and server-side programming languages ​​are »thread« -controlled, i.e. one or more server processes are available and process one task at a time. The performance is limited by the number of available threads. The disadvantage here: If a thread is waiting for a database or has to write to the disk, "it" does nothing else during this time, but blocks further code execution until this action has ended.

Node.js, on the other hand, normally runs in a thread that is in an endlessly running event loop, the "event loop". All of the application's operations are carried out using what are known as "callback functions". If, for example, a request requires a database query or reads from the hard disk, this is carried out in the background. The »event loop« registers this and in the meantime continues with the next request. When the database query is ready, this is reported to the »Event Loop« via the callback function, and the next necessary steps for the delivery of the result are initiated by it. In practice, this means that node.js can process a large number of queries quasi "in parallel", since it does not waste its time unproductive waiting for slower, computationally intensive processes in subsystems.

And what does that bring us? Together with modern browser technologies such as WebSockets, node.js enables the creation of high-performance real-time multi-user web applications. Examples are the multi-player Scrabble game wordsquared or the collaborative writing environment Etherpad Lite. In both applications, inputs and outputs of many users have to be processed in parallel and in real time, which can be achieved with relatively modest server-side hardware requirements thanks to the event-driven architecture of node.js.

If web workers would like to program their server-side applications in JavaScript or have high requirements in terms of "real-time performance" due to the type of application, then node.js can be the tool of choice.

Download and installation

Node.js runs on Unix systems such as Mac OS X or Linux, but also in the Cygwin environment under Windows. As of version 0.6.0, an already compiled, ready-to-use "exe" for Windows is offered for the first time. It is lean and modular in structure, has some important core modules and has npm, a package manager for installing additional modules, similar to Rubygems for Ruby / Rails or Pear for PHP.

On the Mac you can also install node.js with the package manager Homebrew.

"Hello World" - A web server in node.js

A »hello.js« as a web server - the mandatory output of »Hello World« to try out a new language - looks like this:

  1. var http = require ('http');
  2. http.createServer (function (req, res) {
  3. res.writeHead (200, {'Content-Type': 'text / plain'});
  4. res.end ('Hello world! \ n');
  5. }). listen (1337, "");
  6. console.log ('Server running at');

The web worker writes this JS code in a file called »hello.js« and executes it on the command line. Node.js starts the program hello.js, which is a full-fledged web server on port 1337. If he now directs his browser to, the "Hello World" appears in the browser. The »http« module is supplied with node.js and provides a complete web server environment.

Reusable code

In addition to the good performance, the attraction of Node.js lies in the possibility of code reusability. If the web worker writes JavaScript methods for the browser anyway on the client side, e.g. for sophisticated form validation, he can also use these on the server side in Node.js. This saves him from having to implement the application logic in another language (PHP and Co.) for the server.

And he can use existing JS code libraries like jQuery. The module is downloaded with and can be used in Node.js. The web worker can then carry out tasks such as "scraping" information from other websites on the server side and processing DOM trees with the usual jQuery methods.


Of course, that was only a small glimpse into this interesting technology. Some further links: