-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·62 lines (53 loc) · 1.5 KB
/
server.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
61
62
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql2');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const connection = mysql.createConnection({
host: '172.17.0.2',
user: 'root',
password: 'Sachin@9900',
database: 'nodejs'
});
connection.connect(err => {
if (err) {
console.error('Error connecting to database:', err);
return;
}
console.log('Connected to database');
// Create table if it doesn't exist
const createTableQuery = `
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
)
`;
connection.query(createTableQuery, err => {
if (err) {
console.error('Error creating table:', err);
return;
}
console.log('Table created');
});
});
app.post('/register', (req, res) => {
const { name, email, password } = req.body;
const newUser = { name, email, password };
connection.query('INSERT INTO users SET ?', newUser, (err, results) => {
if (err) {
console.error('Error registering user:', err);
res.status(500).send('Error registering user');
return;
}
res.status(200).send('User registered successfully');
});
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/registration.html');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
})