-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.js
60 lines (51 loc) · 1.69 KB
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// form.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
// Middleware to parse JSON bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Route to serve the form
app.get('/', (req, res) => {
res.send(`
<html>
<head>
<title>Node.js Form</title>
</head>
<body>
<h1>Sign Up Form</h1>
<form action="/submit" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" 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>
<button type="submit">Submit</button>
</form>
</body>
</html>
`);
});
// Route to handle form submission
app.post('/submit', (req, res) => {
const { username, email, password } = req.body;
// Simple validation
if (!username || !email || !password) {
return res.status(400).send('All fields are required!');
}
if (password.length < 6) {
return res.status(400).send('Password must be at least 6 characters long.');
}
// Simulate success
console.log('Form Data:', req.body);
res.send(`Form submitted successfully! Welcome, ${username}!`);
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});