Copy Codenpm init -y
Copy Codenpm install express body-parser
index.html
:
Copy Code<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>
</head>
<body>
<h1>Registration Form</h1>
<form action="/register" method="post">
<label for="first-name">First Name:</label>
<input type="text" id="first-name" name="first-name" required><br><br>
<label for="last-name">Last Name:</label>
<input type="text" id="last-name" name="last-name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
server.js
:
Copy Codeconst express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Middleware to parse form data
app.use(bodyParser.urlencoded({ extended: true }));
// Serve the HTML form
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Handle form submission
app.post('/register', (req, res) => {
const { firstName, lastName, email, password } = req.body;
console.log(`First Name: ${firstName}`);
console.log(`Last Name: ${lastName}`);
console.log(`Email: ${email}`);
console.log(`Password: ${password}`);
// You can add logic here to save data to a database or perform other actions.
res.send('Registration successful!');
});
// Start the server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});