Writing Server-side JavaScript with node.js UserPageVisits:369 active 484 694 DaniWeb 561 60 2011-02-17T07:47:19+00:00 https://www.daniweb.com/programming/web-development/reviews/347606/writing-server-side-javascript-with-node-js

Writing Server-side JavaScript with node.js

jeffcogswell
Manufacturer
node.js
URL Screenshot of http://nodejs.org http://nodejs.org
Price
Free/Open Source
Pros
You get to use the JavaScript in both the client side and server side.
Cons
By itself, you have to write an entire web server (but there are additional libraries that handle that)
Summary
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.
Rating

. 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 127.0.0.1 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: http://127.0.0.1:8124/hello/there/everyone

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:

Greetings
/hello/there/everyone

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

I'm a software engineer with about 20 years of experience. In September, October, and November I'll be teaching JavaScript and jQuery courses in Cincinnati, Boston, and New York. Check out my website for details: http://bit.ly/learnJavaScript Find me on Facebook: Profile at facebook.com/jeffcogswell and my training profile at http://bit.ly/cogsfacebook.