
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialIn this topic, we are going to understand the HTTP module. It is used for making a web server to handle requests and provide responses. There are two modules mentioned below:
Both these modules are similar but when we want to work on a secure module then we are going to use https. And for which we need to supply a secure certificate whereas for http this is not needed.
Let's start using https module to make a request to some page say Wikipedia of Sachin Tendulkar who is known as the god of cricket. https://en.wikipedia.org/wiki/Sachin_Tendulkar.
In this URL, the domain is en.wikipedia.org and route is wiki/sachin_tendulkar.
App1:
- Let's create a request.js file and import https module.
var https = require(‘https’);
// since Wikipedia is served in https, we are https. If it has to be another site, which serves http, we would have used http and the rest of the code remains the same.
- Also we need a fs module to stream the response to a file.
var fs = require(‘fs’);
- To make a request we need to build options to it. In our case i.e. Wikipedia
var options = { hostname: ‘en.wikipedia.org’, port: 443, path: ‘/wiki/Sachin_Tendulkar’, method: ‘GET’ }
Let’s make a http request with above options with a callback to accept response.
var request = https.request(options, callback);
- Response is a stream object i.e. our Wikipedia page provided in binary format and lets listen to this stream object i.e. on data event and store it to file system.
var callback = (res) => {
var responseBody = ‘’;
console.log(‘response from server started’);
console.log(`Server status: ${res.statusCode}`);
console.log(`Response headers: ${res.headers}`);
res.setEncoding(‘UTF-8’); // since data is in binary
res.on(‘data’, (chunk) => {
console.log(`--data length--${chunk.lenghth}`);
responseBody = += chunk;
});
res.on(‘end’,() => {
fs.writeFile(‘SachinTendulkar.html’, responseBody, (err) => {
if (err) { throw err;}
console.log(‘File is downloaded’);
});
});
}
- To see if there are any errors, listen to error event on req object
req.on(‘err’, (err) => {
console.log(err.message);
});
- Finally end our request i.e. req.end();
- Run node request.js and observe that new file is created with Sachin-Tendulkar.html
varhttps = require("https");
varfs = require("fs");
varoptions = { hostname:"en.wikipedia.org", port:443, path:"/wiki/Sachin_Tendulkar", method:"GET" };
varreq = https.request(options, function(res) {
varresponseBody = "";
console.log("Response from server started.");
console.log(`Server Status: ${res.statusCode} `);
console.log("Response Headers: %j", res.headers);
res.setEncoding("UTF-8");
res.on("data", function(chunk) {
console.log(`--data length-- ${chunk.length}`);
responseBody += chunk;
});
res.on("end", function() {
fs.writeFile("Sachin-Tendulkar.html", responseBody, function(err) {
if (err) { throwerr; }
console.log("File Downloaded");
});
});
});
req.on("error", function(err) {
console.log(err.message);
});
req.end();
App2: Let's build a web server but this time with http module. So we don’t need a secure certificate.
- Create a server.js file and import http
var http = require(‘http’);
- Let’s create a server using http
var server = http.createServer((req, res) => {
step 3
});
- Here we are going write headers to the response and end on completion.
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello world’);
- Finally, we need to set a port on which we can listen and log serve has started listening
server.listen(3000);
console.log(‘Server is listening on port: 3000’);
- Now run node server and see that server is listening on port 3000
- Try to navigate to localhost:3000 and see how it responds with the plain text ‘Hello world’.
- We can further extend this by writing an HTML content and changing the header to text/html. Also, we can find the request URL and request method from the request object. Based on URL and method we can response accordingly.
Note: If we want to build an https server, we need to provide options to createServer as shown below:
const options = {
key: fs.readFileSync('./agent2-key.pem'),
cert: fs.readFileSync('./agent2-cert.pem')
}
https.createServer(options,(req, res)=>{…});
or
const options ={
pfx: fs.readFileSync('./test_cert.pfx'),
passphrase:'sample'
};
App3: Now build a server to serve files.
- Let's create a fileserver.js and create a folder ‘public’ with few files in it.
- Open fileserver file and import the following modules.
http.createServer((req,rest) => {
console.log(`${req.method} request for ${req.url}`);
}).listen(3000);
console.log("File server running on port 3000");
- Now based on the url, we will serve the files inside the public folder i.e., default page (may be previous sachin-tendulkar.html page downloaded) or image (windows provide default pictures copy one to the public folder say Jellyfish.jpg).
If (req.url === ‘/’) {
fs.readFile("./public/default.html", "UTF-8", function(err, html) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(html);
});
// refer to file system topic to understand this better
} else if (req.url.match(/.jpg$/)) {
var imgPath = path.join(__dirname, 'public', req.url);
var imgStream = fs.createReadStream(imgPath);
res.writeHead(200, {"Content-Type": "image/jpeg"});
imgStream.pipe(res);
// refer to stream topic.
}
For any other page, lets respond with an invalid request
else {
res.writeHead(404, {"Content-Type": "text/plain"});
res.end("404 Not Found");
}
- Run node fileserver and navigate to localhost:3000 and observe the sachin-tendulkar page is rendered.
- If we navigate to localhost:3000/jellyfish.jpg then it should return the image. For other page url then we get 404 Not Found.
varhttp = require("http");
varfs = require("fs");
varpath = require("path");
http.createServer(function(req, res) {
console.log(`${req.method} request for ${req.url}`);
if (req.url === "/") {
fs.readFile("./public/default.html", "UTF-8", function(err, html) {
res.writeHead(200, {"Content-Type":"text/html"});
res.end(html);
});
} elseif (req.url.match(/.jpg$/)) {
varimgPath = path.join(__dirname, 'public', req.url);
varimgStream = fs.createReadStream(imgPath);
res.writeHead(200, {"Content-Type":"image/jpeg"});
imgStream.pipe(res);
} else {
res.writeHead(404, {"Content-Type":"text/plain"});
res.end("404 Not Found");
}
}).listen(3000);
console.log("File server running on port 3000");
The above example can be extended to serve JSON as content type based on URL as shown below:
res.writeHead(200, {"Content-Type": "text/json"});
res.end(JSON.stringify(data))
Here data could be content of the file with JSON extension.
App4:
All the above examples are serving ‘GET’ method. Lets extend it further to serve a FORM this time. Inside this form, we will have submit button and on click of which will do a ‘POST’ method to our server.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fill out this Form</title>
</head>
<body>
<h 1>Fill out this form</h 1>
<form action="/" method="post">
<label for="first">First name</label>
<input type="text" id="first" name="first" required />
<label for="last">Last name</label>
<input type="text" id="last" name="last" required />
<label for="email">Email</label>
<input type="text" id="email" name="email" required />
<button>Submit</button>
</form>
</body>
</html>
- Create a file formserver.js and start a server on port 3000.
var http = require("http");
var fs = require("fs");
http.createServer( (req, res) => {
…
}).listen(3000);
console.log("Form server listening on port 3000");
- Let’s add conditional code based on request method ‘GET’ and ‘POST’.
if (req.method === "GET") {
…
} else if (req.method === "POST") {
…
}
- In case of the GET method, lets respond with the default.html containing FORM mentioned above.
res.writeHead(200, {"Content-Type": "text/html"});
fs.createReadStream("./default.html", "UTF-8").pipe(res);
- With the POST method, listen to data event and end event on request object to capture user data and respond.
var body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", function() {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>Form Results</title>
</head>
<body>
<h 1>Your Form Results</h 1>
<p>${body}</p>
</body>
</html>
`);
});
varhttp = require("http");
varfs = require("fs");
http.createServer(function(req, res) {
if (req.method === "GET") {
res.writeHead(200, {"Content-Type":"text/html"});
fs.createReadStream("./default.html", "UTF-8").pipe(res);
} elseif (req.method === "POST") {
varbody = "";
req.on("data", function(chunk) {
body += chunk;
});
req.on("end", function() {
res.writeHead(200, {"Content-Type":"text/html"});
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>Form Results</title>
</head>
<body>
<h 1>Your Form Results</h 1>
<p>${body}</p>
</body>
</html>
`);
});
}
}).listen(3000);
console.log("Form server listening on port 3000");
On post form submission: