What is Node js?
A lot of the confusion for newcomers to Node is misunderstanding exactly what it is. The description on nodejs.org definitely doesn't help more.
An important thing to realize is that Nodejs is not a webserver. By itself it doesn't do anything. And It doesn't work like Apache. There is no config file where you point it to you HTML files. If you want it to be a HTTP server, you have to write an HTTP server (with the help of its built-in libraries). Node.js is just another way to execute code on your computer. It is very simply a JavaScript runtime.
Installing Node
Node.js is very easy to install. If you're using Windows or Mac, installers are available on the download page.
https://nodejs.org/#download
I've Installed Node, now what?
It's so easy. Here are the exact steps I just took to run the "Hello World" example found at http://nodejs.org/. This is a quick and non understable example. For a permanent installation you'd want to store the executable in a more reasonable place than the root directory and update your PATH to include its location.
Download the Windows executable here: http://nodejs.org/#download
Copy the file to C:\
Create C:\hello.js
Paste in the following content:
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello This is my first node script. Thanks - Arpan Thakrar');
});
server.listen(8080);
console.log('Hey This is my first nodejs script. Thanks - Arpan Thakrar');
Save the file. And Go to the
Start -> Run... -> cmd
c:
C:>node hello.js
Output -:
Hey This is my first nodejs script. Thanks - Arpan Thakrar
Note-:
This is a basic of nodejs. This is not a full-featured HTTP server. It can't serve any HTML file or images. In fact, no matter what you request, it will return 'Hey This is my first nodejs script. Thanks - Arpan Thakrar'. However, you can run this and hit http://localhost:8080 in your browser and you'll see the text.
Hey This is my first nodejs script. Thanks - Arpan Thakrar
You might notice something a little different now. Your Node.js application no longer exits. This is because you created a server and your Node.js application will continue to run and respond to requests until you kill it yourself.
If you want this to be a full-featured web server, then you have to do that work. We will learn that in next post :)NJOY!!
That's it. This was done on Windows.
Very Helpful to know about Node js. Thank you..
ReplyDelete