-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (65 loc) · 2.22 KB
/
server.js
File metadata and controls
77 lines (65 loc) · 2.22 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import express from 'express';
import cors from 'cors';
import mysql from 'mysql2';
import bodyParser from 'body-parser';
import 'dotenv/config';
const app = express();
// the below set up bodyParser to handle data from React or Postman
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// process.env.NAME gets the value of NAME from your .env file
const port = process.env.PORT;
app.use(cors({ origin: 'http://localhost:5173' }));
const db = mysql.createConnection({
host: 'thresholds-test.mysql.database.azure.com',
user: process.env.USERNAME, // Replace with your MySQL username
port: 3306, // Replace with the port you need - may be different from mine
password: process.env.PASSWORD, // Replace with your MySQL password
database: 'tasks', // Replace with your database name
});
db.connect((err) => {
if (err) {
console.error('Error connecting to the database:', err);
return;
}
console.log('Connected to the database');
});
// app.get, .post, .push - these are all set to handle different
// HTTP verbs/methods - we should talk about these
// I like to call these "routes"
app.get('/', (req, res) => {
res.send('Hello World!');
})
app.get('/tasks', (req, res) => {
const query = "SELECT * FROM tasks;";
db.query(query, (err, results) => {
if (err) {
console.log("oh no we did bad");
console.log(err);
res.status(500).json({error: 'Error getting tasks.'})
}
else {
res.json(results);
}
})
})
// new route to add a task to the database
app.post('/tasks', (req, res) => {
const params = [req.body['title'], req.body['description'], req.body['is_completed']];
console.log(req.body)
const query = "INSERT INTO tasks (title, description, is_completed) VALUES (?, ?, ?);"
db.query(query, params, (err, results) => {
if (err) {
console.log("oh no we did bad");
console.log(err);
res.status(500).json({error: 'Error adding task to database.'})
}
else {
res.status(200).json(results);
}
})
})
// starts the app
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})