-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
162 lines (128 loc) · 4.95 KB
/
Copy pathserver.js
File metadata and controls
162 lines (128 loc) · 4.95 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import express, {request, response} from 'express';
import cors from 'cors';
import mysql from 'mysql2';
import bodyParser from 'body-parser';
import 'dotenv/config';
//const db = mysql.createConnection();
// set up the actual express application
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// select the port for your application
//just use 3000 please
const port = process.env.PORT;
app.use(cors({ origin: 'http://localhost:5173' }));
//setting up necessary information for the MySQL server connection
const db = mysql.createConnection ({
host: process.env.SERVERHOST,
user: process.env.SQL_USERNAME, //MySql username (no + no)
port: process.env.SQL_PORT, //Replace with your port number (if not 3306) but 3306 is the default port
password: process.env.PASSWORD, //MySql password
database: process.env.DATABASE //MySql database name
// ssl: { rejectUnauthorized: true } //this ensures an encrypted connection to the database
});
//actually establishes the database connection
db.connect((err) => {
if (err) {
console.log('Error connecting to database: ', err);
return;
}
console.log('Connected to database');
});
//set up a test route
//req is short for request
//res is short for response
//this is a simple route that sends a response back to the user
app.get('/', (req, res) => {
res.send("Hello World");
});
//GET request to get all tasks from the database
//app.get('/x') - x/ is the URL added to the end of localhost:3000
app.get('/tasks', (req,res) => {
//write the querey
const query = "SELECT * FROM tasks; ";
//make query run
db.query(query, (err, results) => {
//handle the query passing in the parameters from the body
if(err) {
console.log(`whoops! could not get tasks, error mesage is ' ${err}'`);
res.status(500).json({error: 'Error retrieving tasks.'});
}
else {
console.log(results[0]);
res.json(results);
}
})
});
app.post('/tasks/add', (req, res) => {
const params = [req.body['title'], req.body['description'], req.body['is_completed']];
const query = "INSERT INTO tasks (title, description, is_completed) VALUES (?, ?, ?);";
db.query(query, params, (err, results) => {
if(err) {
console.log(`whoops! could not add task, error mesage is ' ${err}'`);
res.status(500).json({error: 'Error adding task.'});
}
else {
console.log(results[0]);
res.json({ message: "Task added successfully.", id: results.insertId })
}
});
});
app.put('/tasks/fullUpdate/:id', (req, res) => {
const taskId = req.params.id;
const { title, description, is_completed } = req.body;
const params = [title, description, is_completed, taskId];
const query = "UPDATE tasks SET title = ?, description = ?, is_completed = ? WHERE id = ?;";
db.query(query, params, (err, results) => {
if(err) {
console.log(`whoops! could not update task, error mesage is ' ${err}'`);
res.status(500).json({error: 'Error updating task.'});
}
else {
console.log(results[0]);
res.json(results);
}
});
});
app.delete('/tasks/delete/:id', (req, res) => {
const taskId = req.params.id;
const query = "DELETE FROM tasks WHERE id = ?;";
db.query(query, taskId, (err, results) => {
if(err) {
console.log(`whoops! could not delete task, error mesage is ' ${err}'`);
res.status(500).json({error: 'Error deleting task.'});
} else if (results.affectedRows === 0) {
res.status(404).json({ error: "Task not found." });
} else {
console.log(results[0]);
res.json(results);
}
});
});
//this is a catch all route
//if the user tries to access a route that does not exist
// app.all('*', (req, res) => {
// res.status(404).send('Page not found');
// });
app.patch ('/tasks/partUpdate/:id', (req, res) => {
const taskId = req.params.id;
const { is_completed } = req.body;
const params = [is_completed, taskId];
const query = "UPDATE tasks SET is_completed = ? WHERE id = ?;";
db.query(query, params, (err, results) => {
if(err) {
console.log(`whoops! could not update task, error mesage is ' ${err}'`);
res.status(500).json({error: 'Error updating task.'});
}
else {
console.log(results[0]);
res.json(results);
}
});
});
//actually start the Express server
//this has to be at the end of the file
app.listen(port, () => {
console.log('Server is running on port ' + port);
console.log('Press Ctrl+C to quit.');
});