The classic example here is “Hello World,” detailed on the Node website. Almost everyone starts with Hello World, though, so check that out on your own, and skip straight to something a lot more interesting: a server that can send static files, not just a single line of text:
Thanks much to Mike Amundsen for the pointer to similar code. This particular example was posted by Devon Govett on the Nettuts+ training blog, although it’s been updated for the current version of Node in a number of places. Devon’s entire tutorial post is actually a great companion piece on getting up to speed on Node once you have a handle on the basics.
If you’re new to Node, type this code into a text file and save the file as NodeFileServer.js. Then head out to the Node website and download Node or check it out from the git repository. You’ll need to build the code from source; if you’re new to Unix, make, and configure, then check out the online build instructions for help.
— (bdm0509@Bretts-MacBook-Pro Sun, 29 May 11) —————————— (/Users/bdm0509/tmp/Node/src) — — (09:09 $)-> export PATH=$HOME/local/Node/bin:$PATH — (bdm0509@Bretts-MacBook-Pro Sun, 29 May 11) —————————— (/Users/bdm0509/tmp/Node/src) — — (09:09 $)-> cd ~/examples — (bdm0509@Bretts-MacBook-Pro Sun, 29 May 11) — — — — — — — — — — —— (/Users/bdm0509/examples) — — (09:09 $)-> Node NodeFileServer.js Server running at https://127.0.0.1:1337/
Make sure you still have your NodeFileServer.js code running via Node. Then you can hit your local machine — on port 1337 — and see this unremarkable output.
Yes, this is about as mundane as you can get. Well, that is, until you realize that you’ve actually written a file server in about 20 lines of code. The output you see — the actual code of the script you wrote — isn’t canned in the script itself. It’s being served from the file system. Throw an image into the same directory, and simply add the name of the image to the end of your URL, like https://localhost:8080/my_image.png:
A NodeJS module helps you read large text files, line by line, without buffering the files into memory.
npm install line-by-line
There’s a lot more to talk about around Node than in the actual code that runs a server. Still, it’s worth taking a blisteringly fast cruise through NodeFileServer.js before moving on. Take another look at the code:
Then, the resulting http variable is used to create a server. That server is handed a function block to run when it’s contacted. This particular function ignores the request completely and just writes out a response, in text/plain, saying simply “Hello Worldn”. Pretty straightforward stuff.
In fact, this lays out the standard pattern for Node usage:
1. Define the type of interaction and get a variable for working with that interaction
2. Create a new server (via createServer()).
3. Hand that server a function for handling requests.
The request handling function should include a request …… and a response.
4. Tell the server to start handling requests on a specific port and IP (via listen).
Get Updates on Tech posts, Interview & Certification questions and training schedules