954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Writing Server-side JavaScript with node.js

By Jeff Cogswell on Feb 17th, 2011 7:47 am

. 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: webserver.png

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:

browser1.png

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

Manufacturer: node.js
Product Website: 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: 9

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: