Writing Server-side JavaScript with node.js

Updated jeffcogswell 6 Tallied Votes 415 Views Share
URL Screenshot of http://nodejs.org http://nodejs.org
Free/Open Source
You get to use the JavaScript in both the client side and server side.
By itself, you have to write an entire web server (but there are additional libraries that handle that)
node.js may well be the next big thing in web application development, as it allows you to write server-side code in the same language you use for writing client/browser-side code, JavaScript.

. Some more HTML follows, and then the original requested URL gets written out, mainly as a demonstration.

The original call to createServer returns a server object, and then we call that object's listen method, which you can see in the line second from last. This call says to listen on address via port 8124. (Normally web servers use port 80; I used the same address in the example that came with node for this sample.)

And the final line writes a quick message to the console—this information doesn't get written to the web browser, but rather the console in which this program is running.

To run the program, you go to the directory where you saved it, and type (assuming the code is saved in the file called example.js, and that node is available in the path):

node example.js

Here's the full session running in the Linux command prompt window:

This is the web server itself running. Let's try it out. I opened up a web browser and put in the following URL:

Here's what I saw in my browser:

Notice that I put in the same address and port as specified in the code. When I right-click in the browser and click View Source, here's what I see:


Dani commented: Pretty interesting :) +25
pritaeas commented: Nice tool indeed, I agree with your rating. +7
Airshow commented: Well written and informative article +3
kvprajapati commented: Good post :) +12
F-3000 commented: Well written and interesting +1