14. App Build Posting Data
Path: NodeJs/Node Js Essentials/app-build-posting-data/server.js
const http = require('http');
const url = require('url');
const fs = require('fs');
const querystring = require('querystring');
const server = http.createServer((req, res) => {
const urlparse = url.parse(req.url, true);
if (urlparse.pathname == '/' && req.method == 'POST') {
req.on('data', data => {
const jsondata = JSON.parse(data);
fs.writeFile('output.txt', data, err => {
if (err) {
console.error(err)
return
}
})
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(projects, null, 2));
});
}
});
server.listen(8000);
Path: NodeJs/Node Js Essentials/app-build-routing-2/server.js
const http = require('http');
const server = http.createServer((req, res)=>{
const url = req.url;
if(url === '/hi'){
res.writeHead(200);
res.write('Welcome Home');
}else if (url === '/hello'){
res.write('HI TCSer');
res.writeHead(200);
}else{
res.write('404 File not found error');
res.writeHead(404);
}
return res.end();
});
server.listen(3000);
Path: NodeJs/Node Js Essentials/app-build-routing/router.js
#!/bin/bash
SCORE=0
PASS=0
fail=0
TOTAL_TESTS=1
TEST_1=$( cat router.js | grep -e "createServer" -e "res.writeHead" -e "Hi TCSer" -e "Hi Welcome" -e "Hello Buddy" | wc -l )
if [ $TEST_1 -ge 5 ]
then PASS=$((PASS + 1))
fi;
echo "Total testcases: 1"
echo "Total testcase passed: $PASS"
echo "Total testcase fail: $fail"
SCORE=$(($PASS*100 / $TOTAL_TESTS))
echo "FS_SCORE:$SCORE%"
Path: NodeJs/Node Js Essentials/app-build-server-setup/server.js
const http = require('http');
const server = http.createServer((req, res)=>{
const url = req.url;
if(url === '/hi'){
res.writeHead(200);
res.write('Hi Welcome');
}else if (url === '/hello'){
res.write('Hello Buddy');
res.writeHead(200);
}else{
res.write('404 File not found error');
res.writeHead(404);
}
return res.end();
});
server.listen(3000);
15. Events HTTP Module
Path: NodeJs/Node Js Essentials/events-http-module/app.js
const http = require('http');
const fs = require('fs')
const server = http.createServer((req, res)=>{
fs.readFile('sample.html', 'utf8' , (err, data) => {
if (err) {
console.error(err)
return
}
res.write(data);
res.end();
})
});
server.listen(8000);
Path: NodeJs/Node Js Essentials/events-http-module/sample.html
<!DOCTYPE html>
<html lang="en">
<body>
<h2>Welcome ! what would oyu like to have</h2>
<ul>
<li>Cofee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>
Path: NodeJs/Node Js Essentials/events-simple-http-module/app.js
const http = require('http');
const server = http.createServer((req, res)=>{
res.write('Hello World');
res.end();
})
});
server.listen(8000);
Path: NodeJs/Node Js Essentials/events-working-with-custom-events/app.js
var events = require('events');
var eventEmitter = new events.EventEmitter();
//Create an event handler:
var Myfunc = function () {
console.log('HI THERE ! HAPPY LEARNING');
}
//Assign the event handler to an event:
eventEmitter.on('MyEvent', Myfunc);
//Fire the 'scream' event:
eventEmitter.emit('MyEvent');
16. Handling Requests
Path: NodeJs/Node Js Essentials/handling-requests/App.js
var http = require('http');
// Create a server object
http.createServer(function (req, res) {
// http header
res.writeHead(200, {'Content-Type': 'text/html'});
var url = req.url;
if(url ==='/') {
res.write(`
<form method="post" action="submit">
<input type=text name=first>
<input type=text name=first>
<button>Submit</button>
</form>
`);
res.end();
}
else if(url ==='/contact') {
res.write(' Welcome to contact us page');
res.end();
}
else {
res.write('Hello World!');
res.end();
}
}).listen(3000, function() {
// The server object listens on port 3000
console.log("server start at port 3000");
});
Path: NodeJs/Node Js Essentials/handling-requests/handlers.js
var http = require('http');
// Create a server object
http.createServer(function (req, res) {
// http header
res.writeHead(200, {'Content-Type': 'text/html'});
var url = req.url;
if(url ==='/') {
res.write(`
<form method="post" action="submit">
<input type=text name=first>
<input type=text name=first>
<button>Submit</button>
</form>
`);
res.end();
}
else if(url ==='/contact') {
res.write(' Welcome to contact us page');
res.end();
}
else {
res.write('Hello World!');
res.end();
}
}).listen(3000, function() {
// The server object listens on port 3000
console.log("server start at port 3000");
});
Path: NodeJs/Node Js Essentials/handling-requests/routers.js
var http = require('http');
// Create a server object
http.createServer(function (req, res) {
// http header
res.writeHead(200, {'Content-Type': 'text/html'});
var url = req.url;
if(url ==='/') {
res.write(`
<form method="post" action="submit">
<input type=text name=first>
<input type=text name=first>
<button>Submit</button>
</form>
`);
res.end();
}
else if(url ==='/contact') {
res.write(' Welcome to contact us page');
res.end();
}
else {
res.write('Hello World!');
res.end();
}
}).listen(3000, function() {
// The server object listens on port 3000
console.log("server start at port 3000");
});
Path: NodeJs/Node Js Essentials/handling-requests/server.js
var http = require('http');
// Create a server object
http.createServer(function (req, res) {
// http header
res.writeHead(200, {'Content-Type': 'text/html'});
var url = req.url;
if(url ==='/') {
res.write(`
<form method="post" action="submit">
<input type=text name=first>
<input type=text name=first>
<button>Submit</button>
</form>
`);
res.end();
}
else if(url ==='/contact') {
res.write(' Welcome to contact us page');
res.end();
}
else {
res.write('Hello World!');
res.end();
}
}).listen(3000, function() {
// The server object listens on port 3000
console.log("server start at port 3000");
});
17. Https Requests
Path: NodeJs/Node Js Essentials/https-requests/app.js
const https = require('https')
const options = {
hostname: 'en.wikipedia.org',
port: 443,
path: '/dwiki/Nodejs',
method: 'GET'
}
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.end()
Extras
18. Addition
Path: NodeJs/Node Js Essentials/addition.js
console.log(parseInt(process.argv[2])+parseInt(process.argv[3]));
19. Callback
Path: NodeJs/Node Js Essentials/callback.js
let sum=0;
for(let i=3; i<=1000; i++){
if(i%3==0){
sum += i
}
if(i%5==0){
sum+=i
}
}
setTimeout(()=>console.log(sum), 1000);
20. Module
Path: NodeJs/Node Js Essentials/module.js
// app.js
const m = require('./module');
const readline = require('readline');
let r1 = readline.createInterface(process.stdin, process.stdout);
r1.question("first number", n1 =>{
r1.question("second number", n2 =>{
console.log(m.add(n1,n2));
console.log(m.multiply(n1,n2));
})
})
// module.js
module.exports.add = (a,b) => {
return a+b;
}
module.exports.multiply = (a,b)=>{
return a+b
}
20. Timer 1
Path: NodeJs/Node Js Essentials/timer1.js
function print(){
console.log("TCS");
}
setInterval(print, 5000);
21. Timer 2
Path: NodeJs/Node Js Essentials/timer2.js
function print(){
console.log("TCS");
}
setTimeout(print, 2000);
22. Timer 3
Path: NodeJs/Node Js Essentials/timer3.js
function print(){
console.log("TCS");
}
function stop(){
clearInterval(t)
}
const t = setInterval(print, 2000);
setTimeout(stop, 10000);