Build WebServer With Node.js
Node.js is a javascript server side framework. It means we can execute javascript code at the serverside and aslo all client requests will be processed with javascript. It is asynchronous. It means when you are doing server side i/o related operations still parallely it can handle other requests. Hence it can handle heavy load traffic.
Prerequisites:
1. Download and Install Node.js from here.
2. Any Editor like Notepad/Notepad++/Visual Studio Code.
How to Create a WebServer:
Step #1:
Write the below code into a notepad and save it with some name like index.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(5000);
In the above code you can see req and res parameters. The req object contains all the details related the incoming http request. The res is object which will be returned as a response to the incoming request.
require() is the function in node.js is used to refer any modules or external files. http is a inbuilt module in node.js.
Step #2:
Open command prompt and go to the location where the above file is saved and run the below command.
node <filename>
After executing the above command then your web server will be up and running.
'
Step #3:
In the above javascript code we have given port number 5000. So open browser and type the below url.
You should see Hellow World!.
In the same way you can send any response from javascript. Some times we may need to bring data from database and return that data as a response. So you can do that as below.
How get data from Database in Node.Js:
Here i am using mysql database. But you can use any database of your choice. So to use mysql we need to install the database drivers. In Node.js npm acts as a package manager. It is similar to nuget in .net.
Step #1: Install mysql driver.
npm install mysql
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("Query you want to execute", function (err, result) {
if (err) throw err;
console.log(result);
});
});
In the above code you can see that you are supposed to mention your database username and password. And in the connect function you can a placeholder where you can write any sql query to execute.
Step #2:
Save the above code with .js extension like SqlQueryWithNode.js.
Step#3:
Run the below command to execute in command prompt.
node SqlQueryWithNode.js
Prerequisites:
1. Download and Install Node.js from here.
2. Any Editor like Notepad/Notepad++/Visual Studio Code.
How to Create a WebServer:
Step #1:
Write the below code into a notepad and save it with some name like index.js
In the above code you can see req and res parameters. The req object contains all the details related the incoming http request. The res is object which will be returned as a response to the incoming request.
require() is the function in node.js is used to refer any modules or external files. http is a inbuilt module in node.js.
Step #2:
Open command prompt and go to the location where the above file is saved and run the below command.
node <filename>
After executing the above command then your web server will be up and running.
'
Step #3:
In the above javascript code we have given port number 5000. So open browser and type the below url.
You should see Hellow World!.
In the same way you can send any response from javascript. Some times we may need to bring data from database and return that data as a response. So you can do that as below.
How get data from Database in Node.Js:
Here i am using mysql database. But you can use any database of your choice. So to use mysql we need to install the database drivers. In Node.js npm acts as a package manager. It is similar to nuget in .net.
Step #1: Install mysql driver.
npm install mysql
In the above code you can see that you are supposed to mention your database username and password. And in the connect function you can a placeholder where you can write any sql query to execute.
Step #2:
Save the above code with .js extension like SqlQueryWithNode.js.
Step#3:
Run the below command to execute in command prompt.
node SqlQueryWithNode.js
Comments
Post a Comment