-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
38 lines (31 loc) · 873 Bytes
/
app.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
const express = require('express');
const mysql = require('mysql2');
const app = express();
const port = 3000;
app.use(express.urlencoded({ extended: true }));
const db = mysql.createConnection({
host: 'mysql', // Docker service name for the MySQL container
user: 'vishal',
password: '1',
database: 'taskdb',
port: 3306
});
db.connect((err) => {
if (err) throw err;
console.log('Connected to MySQL');
});
app.get('/', (req, res) => {
res.sendFile('/index.html', { root: __dirname });
});
app.post('/addTask', (req, res) => {
const task = req.body.task;
const sql = 'INSERT INTO tasks (description) VALUES (?)';
db.query(sql, [task], (err, result) => {
if (err) throw err;
console.log('Task added');
res.send('Task added successfully');
});
});
app.listen(port, () => {
console.log(`App running at http://localhost:${port}`);
});