-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (38 loc) · 1.11 KB
/
index.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
const express = require('express');
const mysql = require('mysql');
const app = express();
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'customerinfo'
});
connection.connect(err => {
if (err) {
console.error('Error connecting to MySQL: ' + err.stack);
return;
}
console.log('Connected to MySQL as id ' + connection.threadId);
});
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('form');
});
app.post('/submit', (req, res) => {
const { name, email, phone } = req.body;
const customer = { name, email, phone };
connection.query('INSERT INTO customers SET ?', customer, (err, result) => {
if (err) {
console.error('Error inserting customer: ' + err.stack);
res.send('Error inserting customer');
return;
}
console.log('Customer inserted with ID ' + result.insertId);
res.send('Customer inserted successfully');
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log('Server is running on port ' + PORT);
});