Introduction
Ankit kumar sahu
Developer @ Freelance | Web Development, MERN Stack, React.js, c++
What is node.js?
Why Node.js?
A common task for a web server can be to open a file on the server and return the content to the client
Here is how PHP and ASP handle a file request -:
Here is how node.js handles a file request -:
What can Node.js do?
Getting started with Node.js
Installation
Install it from the official website of Node.js as per the instruction given
Getting started
Let’s try to display “Hello World” in the web browser
Create a Node.js file and add the following code to it -:
var http = require('http');
var port = 5500; //your localport
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type' : 'text/html'});
res.end('Hello World');
}).listen(port);
Save the code as myFirst.js in
“C:\Users\Your name\myFirst.js”
The above code tells the computer to write ‘Hello World’ if anyone tries to access your computer on port 5500.
Initiate the Node.js file
Node.js file must be initiated in the cmd program of your computer
Strat the cmd and navigate to the folder where you stored your myFirst.js file
then in the cmd write node myFirst.js
C:\Users\Your name>node myFirst.js
now your computer works as a server.
If anyone tries t access your computer on port 5500 they will get a “Hello World” message in return.
Start your browser and type in the address :