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 would like to become a Node.js Certified professional, then visit Mindmajix - A Global online training platform: "Node.js Training and Certification Course". This course will help you to achieve excellence in this domain.
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.
Checkout Node.JS Interview Questions
— (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 World”. Pretty straightforward stuff.
In fact, this lays out the standard pattern for Node usage:
Explore Node.js Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download Now!
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
|Node.JS Training||Apr 01 to Apr 16|
|Node.JS Training||Apr 04 to Apr 19|
|Node.JS Training||Apr 08 to Apr 23|
|Node.JS Training||Apr 11 to Apr 26|
Priyanka Vatsa is a Senior Content writer with more than five years’ worth of experience in writing for Mindmajix on various IT platforms such as Palo Alto Networks, Microsoft Dynamics 365, Siebel, CCNA, Git, and Nodejs. She was involved in projects on these technologies in the past, and now, she regularly produces content on them. Reach out to her via LinkedIn and Twitter.
Copyright © 2013 - 2023 MindMajix Technologies