Web Project
Web Project
Input:
const http = require('http');
const message = `
<html>
<head>
<title>Node.js Message</title>
</head>
<body style="display: flex; justify-content:
center; align-items: center; height: 100vh;
background-color: #222; color: white; text-align:
center;">
<div>
<h1 style="color: #4CAF50; font-size:
3em;">Hello Everyone</h1>
<p style="font-size: 1.5em;">This is
a message served by a Node.js server.</p>
</div>
</body>
</html>
`;
res.end(message);
});
server.listen(3000, () => {
console.log('Server running at
http://localhost:3000');
});
Output:
2.Display the current date and time in the browser.
INPUT:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>Live Digital Clock</title>
<style>
body {
background: linear-gradient(45deg,
#ec2654, #e6543a, #3894f0);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
color: white;
text-align: center;
}
.clock {
font-size: 60px;
font-weight: bold;
background: rgba(0, 0, 0, 0.3);
padding: 20px 40px;
border-radius: 15px;
box-shadow: 0px 0px 15px rgba(0, 0, 0,
0.3);
}
</style>
</head>
<body>
<div class="clock" id="clock">00:00:00</div>
<script>
function updateClock() {
const now = new Date();
let hours =
now.getHours().toString().padStart(2, '0');
let minutes =
now.getMinutes().toString().padStart(2, '0');
let seconds =
now.getSeconds().toString().padStart(2, '0');
document.getElementById('clock').innerText = `$
{hours}:${minutes}:${seconds}`;
}
setInterval(updateClock, 1000);
updateClock(); // Initial call to prevent 1-
second delay
</script>
</body>
</html>
OUTPUT:
3.Pass a query string in URL
INPUT:
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
if (query.name && query.age) {
} else {
<code>http://localhost:3000/?name=John&age=25</code></p>`);
});
});
OUTPUT:
const webAddress =
'https://example.com:8080/path/to/page?
name=john&age=30#section1';
console.log('Protocol:', parsedUrl.protocol);
console.log('Host:', parsedUrl.host);
console.log('Hostname:', parsedUrl.hostname);
console.log('Port:', parsedUrl.port);
console.log('Pathname:', parsedUrl.pathname);
console.log('Search Params:', parsedUrl.search);
console.log('Hash:', parsedUrl.hash);
const searchParams =
Object.fromEntries(parsedUrl.searchParams.entries());
console.log('Query Parameters:', searchParams);
OUTPUT:
Protocol: https:
Host: example.com:8080
Hostname: example.com
Port: 8080
Pathname: /path/to/page
Hash: #section1
try {
throw new CustomError("User not found", 404);
} catch (error) {
console.error(`Error: ${error.message} (Status
Code: ${error.statusCode})`);
}
OUTPUT:
6.Event handling
INPUT:
const EventEmitter = require('events');
orderProcessor.placeOrder(101, "Alice");
OUTPUT:
INPUT:
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(requestLogger);
app.use(authMiddleware);
app.use('/api', apiRouter);
app.use(express.static(path.join(__dirname, 'public')));
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
OUTPUT:
GET / - 2025-03-03T12:03:40.136Z
INPUT:
const express = require('express');
const app = express();
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
OUTPUT:
Input:
const express = require('express');
const app = express();
OUTPUT:
{"message":"Hello, this is an Express
API!"}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
OUTPUT:
INPUT:
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.json());
OUTPUT:
6. Using middleware to log request details.
INPUT:
const express = require('express');
const app = express();
app.use(requestLogger);
OUTPUT:
INPUT:
const express = require('express');
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
text-align: center;
background-color: #f0f0f0;
padding: 50px;
h1 {
color: #333;
button {
background-color: #007bff;
color: white;
border: none;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
button:hover {
background-color: #0056b3;
</style>
</head>
<body>
<script>
function showMessage() {
</script>
</body>
</html>
`);
});
app.listen(PORT, () => {
});
OUTPUT:
INPUT:
const express = require('express');
const app = express();
const users = [
{ id: 1, name: "Alice", email: "[email protected]" },
{ id: 2, name: "Bob", email: "[email protected]" },
{ id: 3, name: "Charlie", email: "[email protected]" }
];
OUTPUT:
[{"id":1,"name":"Alice","email":"[email protected]"},
{"id":2,"name":"Bob","email":"[email protected]"},
{"id":3,"name":"Charlie","email":"[email protected]"}]