Node
Node
Introduction
• Node.js was developed by Ryan Dahl in 2009
• Node.js is an open source server framework
• Node.js allows you to run JavaScript on the server
• Node.js runs on various platforms
• Node.js uses asynchronous programming
• Node.js is a server-side platform built on Google Chrome's JavaScript Engine
(V8 Engine)
• Node.js = Runtime Environment + JavaScript Library
Architecture
A common task for a Web server can be to open a file on the server and return
the content to the client. For example php, asp or jsp handles a file request in the
following sequence:
• Sends the task to the computer's file system.
• Waits while the file system opens and reads the file.
• Returns the content to the client.
• Ready to handle the next request.
Components of Node.js
Node.js eliminates the waiting, and simply continues with the next request.
Node.js runs single-threaded, non-blocking, asynchronously programming, which is
very memory efficient.
Node JS Modules
• Modules are a set of functions to be consumed in an application.
• Modules can be user defined or built in (several modules are available).
e.g. var tmp=require(‘http’);
“http” is a module for processing http request and responses.
To load a module, the function “require” is used.
Example
To create a user defined module (save as module1.js)
exports.myDateTime = function () {
return Date();
};
http Module
• Node.js has a built-in module called HTTP, which allows Node.js to transfer
data over the Hyper Text Transfer Protocol (HTTP).
• The HTTP module can create an HTTP server that listens to server ports and
gives a response back to the client.
• The function passed into the http.createServer() method, will be executed
when the client tries to access the computer on port 9999.
• If the response from the HTTP server is supposed to be displayed as HTML,
the HTTP header should be specified.
• The function passed into the http.createServer() has a req argument that
represents the request from the client, as an object (http.IncomingMessage
object).
• This object has a property called "url" which holds the part of the url that
comes after the domain name
Example 3:
var http = require('http');
var url = require('url');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
res.write("<br>");
var q = url.parse(req.url, true).query;
var txt = q.year + " " + q.month;
res.end(txt);
}).listen(9999);
// To execute: http://localhost:9999/example3.js?year=2018&month=April
Example 4:
var http = require('http');
var uc = require('upper-case');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(uc("cse!"));
res.end();
}).listen(9999);
Events
Node.js is perfect for event-driven applications.
Objects in Node.js can fire events, like the readStream object fires events when
opening and closing a file
Example:
var fs = require('fs');
var readStream = fs.createReadStream('./example4.js');
/*Write to the console when the file is opened:*/
readStream.on('open', function () {
console.log('The file is open');
});
Node.js has a built-in module, called "Events", which performs create-, fire-, and
listen for- user defined events.
Install XAMPP Server. Server to run in localhost and use the port 3306
Module: mysql
Server: xampp
Result Object:
fieldCount
affectedRows
insertId
serverStatus
warningCount
message -> Records, Duplicated, Warnings
protocol41
changedRows
Example: Select
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "nodejsdb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
// SELECT * FROM programmes
con.query("SELECT school, deptt FROM programmes", function (err, result, fields)
{
if (err) throw err;
console.log(result)
console.log(result[2].school);
console.log(fields);
console.log(fields[1].name);
});
});
JOINS
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "nodejsdb" });
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql="SELECT programmes.school, student.regno, student.name FROM
programmes JOIN student ON student.deptt = programmes.deptt";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
}); });