Developing Applications using NodeJS.
JavaScript has been a popular language for web development, mainly in the client side (i,e) only on the browser ,not in the server side scripting. So here you can use NodeJS to create your web application in the server side also, not only server side scripting you can even write shell scripting also and lot more. I will one by one in my future blog article.
Now lets look a simple NodeJS program to display Hello World in the web browser. All you need to know is, HTML and JavaScript.
So first you need to install NodeJS in your system. You can find installations procedure Installing NodeJS.
So lets start doing, Here i have included the source code
var http = require('http'); var wel = "Hello World"; var str = "Welcome to Node JS World !!"; var server = http.createServer(function(req, res){ res.writeHead(200,{'Content-Type':'text/html'}); res.write('<!DOCTYPE html><html><head><title>My first nodejs program</title></head>'); res.write('<body>'); res.write('<h1>'+wel+'</h1>'); res.write('<p>'+str+'</p>'); res.write('</body></html>'); res.end(); }); server.listen(8000); console.log('Connect to http://localhost:8000');
In the line 1 : I declared variable http and it is including http libraries, this will be used later to create server.
In the line 2-3 : two string variables holding 2 strings.
Line 5 : Creating Server using http (which was included at line 1). create server Function takes two argument Request and Response,
Line 6-7 : Adding HTTP header to the browser when new request comes, with the help of response.
same as that you can insert your html code using res.write. At the end after the end of your html tag , you have say that response is ended res.end().
Line 14: server.listen(8000) : You are saying the server to listen to port number 8000, If your server and client is the same machine then open your browser after executing the program, type http://localhost:8000 in your browser , you will see the html page.
Line 15: console.log(“connect “) this will print in the terminal, this statement is like printf , cout, echo in other language.
Thats it you have successfully started Node JS . I will keep posting new article so that you can get familiarize in NodeJS.
Node JS from Udemy – complete tutorial
If you found this post interesting Share: