Skip to content

Commit

Permalink
Leacture 3 & 4
Browse files Browse the repository at this point in the history
  • Loading branch information
omprashantjain committed Sep 23, 2024
1 parent 0faf0ec commit ad0d4bd
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Lecture 3 - First Node Server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const http = require('http');

const server = http.createServer((req, res) => {
console.log(req);
});

const PORT = 3001;
server.listen(PORT, () => {
console.log(`Server running on address http://localhost:${PORT}`);
});
32 changes: 32 additions & 0 deletions Lecture 4 - Request and Reponse/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const http = require('http');

const server = http.createServer((req, res) => {
console.log(req.url, req.method, req.headers);

if (req.url === '/') {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>Complete Coding</title></head>');
res.write('<body><h1>Welcome to Home</h1></body>');
res.write('</html>');
return res.end();
} else if (req.url === '/products') {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>Complete Coding</title></head>');
res.write('<body><h1>Checkout our products</h1></body>');
res.write('</html>');
return res.end();
}
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>Complete Coding</title></head>');
res.write('<body><h1>Like / Share / Subscribe</h1></body>');
res.write('</html>');
res.end();
});

const PORT = 3001;
server.listen(PORT, () => {
console.log(`Server running on address http://localhost:${PORT}`);
});
48 changes: 48 additions & 0 deletions Lecture 4 - Request and Reponse/practise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const http = require('http');

const server = http.createServer((req, res) => {
console.log(req.url, req.method);
if (req.url === '/home') {
res.write('<h1>Welcome to Home</h1>');
return res.end();
} else if (req.url === '/men') {
res.write('<h1>Welcome to Men</h1>');
return res.end();
} else if (req.url === '/women') {
res.write('<h1>Welcome to Women</h1>');
return res.end();
} else if (req.url === '/kids') {
res.write('<h1>Welcome to Kids</h1>');
return res.end();
} else if (req.url === '/cart') {
res.write('<h1>Welcome to Cart</h1>');
return res.end();
}


res.write(`
<html lang="en">
<head>
<title>Myntra</title>
</head>
<body>
<head>
<nav>
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/men">Men</a></li>
<li><a href="/women">Women</a></li>
<li><a href="/kids">Kids</a></li>
<li><a href="/cart">Cart</a></li>
</ul>
</nav>
</head>
</body>
</html>
`);
res.end();
});

server.listen(3001, () => {
console.log('Server running on address http://localhost:3001');
});
19 changes: 19 additions & 0 deletions Lecture 4 - Request and Reponse/temp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Myntra</title>
</head>
<body>
<head>
<nav>
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/men">Men</a></li>
<li><a href="/women">Women</a></li>
<li><a href="/kids">Kids</a></li>
<li><a href="/cart">🛒</a></li>
</ul>
</nav>
</head>
</body>
</html>
42 changes: 42 additions & 0 deletions Lecture 4 - Request and Reponse/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
console.log(req.url, req.method, req.headers);

if (req.url === '/') {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>Complete Coding</title></head>');
res.write('<body><h1>Enter Your Details:</h1>');
res.write('<form method="POST" action="submit-details">');
res.write('<input type="text" name="username" placeholder="Enter your name"><br>');
res.write('<input type="radio" name="gender" id="male" value="male" />');
res.write('<label for="male">Male</label>')
res.write('<input type="radio" name="gender" id="female" value="female" />');
res.write('<label for="female">Female</label>');
res.write('<br><button type="submit">Submit</button>');
res.write('</form>');
res.write('</body>');
res.write('</html>');
return res.end();

} else if (req.url === '/submit-details' &&
req.method === 'POST') {
fs.writeFileSync('user.txt', 'Prashant Jain');
res.statusCode = 302;
res.setHeader('Location', '/');
return res.end();
}
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>Complete Coding</title></head>');
res.write('<body><h1>Like / Share / Subscribe</h1></body>');
res.write('</html>');
res.end();
});

const PORT = 3001;
server.listen(PORT, () => {
console.log(`Server running on address http://localhost:${PORT}`);
});
1 change: 1 addition & 0 deletions Lecture 4 - Request and Reponse/user.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prashant Jain

0 comments on commit ad0d4bd

Please sign in to comment.