-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (50 loc) · 1.63 KB
/
server.js
File metadata and controls
59 lines (50 loc) · 1.63 KB
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
const express = require('express');
const mariadb = require('mariadb');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Set up views and view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
// Create a MariaDB connection pool
const pool = mariadb.createPool({
host: 'localhost',
user: 'root',
password: 'my_password',
database: 'my_database',
connectionLimit: 10
});
// Middleware to parse form data
app.use(bodyParser.urlencoded({ extended: true }));
// Serve the HTML form
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'));
});
// Handle form submission
app.post('/join', async (req, res) => {
const { name, password, phone, email } = req.body;
let connection;
try {
connection = await pool.getConnection();
const query = 'INSERT INTO members (name, password, phone, email) VALUES (?, ?, ?, ?)';
await connection.query(query, [name, password, phone, email]);
res.send(`
<h3>Joined Successfully</h3>
<p>Name: ${name}</p>
<p>Password: ${password}</p>
<p>Phone: ${phone}</p>
<p>Email: ${email}</p>
`);
} catch (err) {
console.error(err);
res.status(500).send('Failed to join. Please try again later.');
} finally {
if (connection) connection.release();
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});