-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (43 loc) · 1.46 KB
/
server.js
File metadata and controls
55 lines (43 loc) · 1.46 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const mysql = require('mysql2/promise');
const path = require('path');
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const port = process.env.PORT || 3000;
app.listen(port,'localhost', () => {
console.log(`Server is running on port ${port}`);
});
const dbConfig = {
host: 'localhost',
user: 'root',
password: '',
database: 'telesto',
port: 3306,
};
// Create the connection pool
const pool = mysql.createPool(dbConfig);
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname,'add_user.html'));
});
app.post('/', async (req, res) => {
const email = req.body.email;
const role = req.body.role;
if (email === undefined || role === undefined) {
return res.status(400).json({ error: 'Both email and role are required' });
}
const sqlQuery = 'INSERT INTO admin (email,password,role,status) VALUES (?,NULL,?,"Active")';
try {
const connection = await pool.getConnection();
const [rows, fields] = await connection.execute(sqlQuery, [email, role]);
connection.release();
console.log("member added successfully");
res.status(200).json({ message: 'User added successfully' });
} catch (err) {
console.error('Database error:', err);
res.status(500).json({ error: err.message });
}
});