Slip22 Using node js create an Employee Registration Form validation.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Employee Registration</title>
</head>
<body>
<h1>Employee Registration Form</h1>
<form action="/register" method="post">
<label>Name:</label><br>
<input type="text" name="name" required><br>
<label>Email:</label><br>
<input type="email" name="email" required><br>
<label>Password:</label><br>
<input type="password" name="password" required><br>
<input type="submit" value="Register">
</form>
</body>
</html>
slip22.js
const http = require('http');
const fs = require('fs');
const qs =require('querystring');
const server = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
// Serve HTML form
fs.readFile('index.html', (err, data) => {
if (err) {
res.writeHead(500);
res.end('Internal Server Error');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
});
} else if (req.method === 'POST' && req.url === '/register') {
// Handle form submission
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const formData =qs.parse(body);
const name = formData['name'];
const email = formData['email'];
const password = formData['password'];
// Basic form validation
if (!name || !email || !password) {
res.writeHead(400);
res.end('Please fill out all fields');
} else {
// If validation passes, process registration
res.writeHead(200);
res.end('Registration successful!');
}
});
} else {
// Handle other routes
res.writeHead(404);
res.end('Not Found');
}
}).listen(3032, () => {
console.log('Server is running on port 3032');
});
Comments
Post a Comment