Node JS Basic
Functional expression
function greet(){
console.log("hello welcome");
}
greet();
var anynomousfunction = function (){
console.log("hello This is anynomous function ");
}
anynomousfunction ();
-------------
pass function as param
function doGreet(func) {
func();
}
doGreet( anynomousfunction );
-----------------
Module and require
data.js
var counter= function(arr) {return 'There are' + arr.length . ' element ';
}
var adder = function(){
return ` the sum is $(a+b) `;
}
var PI = 3.1416;
////// module.exports = counter; ///one
module.exports.counter = counter;
module.exports.added = added;
module.exports.PI= PI;
or we can do this
module.exports = {
counter : counter,
PI:PI,
adder: adder
}
app.js
var data= require('./data'); // is {module.exports}
console.log(data.counter(['a','b','c'] ));
console.log(data.adder(5, 6 ));
----------------------
Event emitter
var events = require("events");var myEmitter = new events.EventEmitter();
myEmitter.on( 'someEvent' , function(msg){
console.log(msg);
});
myEmitter.emit( 'someEvent' , function(msg) {
});
------------------
var events = require("events");
var util = require("util");
var Person = function (name) {
this.name = name;
}
util.inherits(Person, events.EventEmitter );
var james = new Person("james");
var mary = new Person("mary");
var people = [james, mary];
people.forEach( function( person){
person.on ( 'speak', function(msg){
console.log(person.name + "said" + msg);
});
});
james.emit("speak", "hey dudes");
mary.emit("speak", "cool");
-------------------
Reading and writing files
//file system module for node js
var fs = require("fs);
// read file synchronous
var readMe = fs.readFileSync( "readme.txt", "utf8");
console.log(readMe);
// read/write file normal
fs.readFile( "readme.txt", "utf8", function(err, data){
fs.writeFile("newFile.txt", readMe);
});
// write file synchronous
fs.writeFileSync("newFile.txt", readMe); //filename , data to write
----------------------
Create and remove directory
var fs = require("fs);
//remove file
fs.unlink("filename");
//synchronous
fs.mkdirSync("dirname");
fs.rmdirSync("dirname");
//asynchronous
fs.mkdir("dirname", function (){
});
fs.rmdir("dirname", function (){
});
// to remove dir dir should be empty
//delete file using unlink
------------------------
Client and server
var http = require("http");var server = http.createServer(function( req, res ){
console.log("request is made"+ req.url);
res.writeHead(200, {"Content-Type": "text/plain" });
res.end(" hey ninjas");
});
server.listen(3000, "127.0.0.1" );
console.log(" now listenung to port 3000");
---------------
Header
conteny-Type
Status
-------------------------
Stream and buffer
//reading streams
//chunk > buffer > stream
//chunk > buffer > stream
var http = require("http");
var fs = require("fs");
var myReadStream = fs.createReadStream(__dirname + "/readMe.txt", "utf8" );
var myWriteStream = fs.createWriteStream(__dirname + "/writeMe.txt", "utf8" );myReadStream.on("data", function( chunk) {
console.log("new chunk received");
console.log(chunk);
myWriteStream.write(chunk);
});
PIPES
myReadStream.pipe(myWriteStream);
// pipe to res in for server
myReadStream.pipe(res);
Serving HTML page
var server = http.createServer(function( req, res ){
console.log("request is made"+ req.url);
res.writeHead(200, {"Content-Type": "text/html" });
});
console.log("request is made"+ req.url);
res.writeHead(200, {"Content-Type": "text/html" });
var myReadStream = fs.createReadStream(__dirname + "/index.html", "utf8" );
myReadStream.pipe(res);
Serving JSON
var server = http.createServer(function( req, res ){
console.log("request is made"+ req.url);
res.writeHead(200, {"Content-Type": "text/json" });
var myObj = {
name: 'Niral',
job : 'developer',
}
});
console.log("request is made"+ req.url);
res.writeHead(200, {"Content-Type": "text/json" });
var myObj = {
name: 'Niral',
job : 'developer',
}
res.end(JSON.stringify(myObj));
Basic Routing
var server = http.createServer(function( req, res ){
console.log("request is made"+ req.url);
if(res.url === "/home" || res.url === "/") {
res.writeHead(200, {"Content-Type": "text/html" });
fs.createReadStream(__dirname + "/index.html" ).pipe(res);
}else if(res.url === "/contact") {
res.writeHead(200, {"Content-Type": "text/html" });
fs.createReadStream(__dirname + "/contact.html" ).pipe(res);
} else if(res.url === "/api/ninjas") {
res.writeHead(200, {"Content-Type": "application/json" });
var myObj = {
name: 'Niral',
job : 'developer',
}
res.end(JSON.stringify(myObj));
}});
---------------------------
Thanks to
The net ninja
https://www.youtube.com/playlist?list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp