mirror of
https://github.com/mariadb-corporation/dev-example-blog-samples.git
synced 2025-07-20 16:54:37 +00:00
Update server.js
This commit is contained in:
@ -9,56 +9,43 @@ app.use(bodyParser.urlencoded({ extended: false }));
|
||||
|
||||
// GET
|
||||
app.get('/tasks', async (req, res) => {
|
||||
let conn;
|
||||
try {
|
||||
const result = await db.pool.query("select * from tasks");
|
||||
res.send(result);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) return conn.release();
|
||||
}
|
||||
});
|
||||
|
||||
// POST
|
||||
app.post('/tasks', async (req, res) => {
|
||||
let task = req.body;
|
||||
console.log(task);
|
||||
let conn;
|
||||
try {
|
||||
const result = await db.pool.query("insert into tasks (description) values (?)", [task.description]);
|
||||
res.send(result);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) return conn.release();
|
||||
}
|
||||
});
|
||||
|
||||
app.put('/tasks', async (req, res) => {
|
||||
let task = req.body;
|
||||
let conn;
|
||||
try {
|
||||
const result = await db.pool.query("update tasks set description = ?, completed = ? where id = ?", [task.description, task.completed, task.id]);
|
||||
res.send(result);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) return conn.release();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/tasks', async (req, res) => {
|
||||
let id = req.query.id;
|
||||
let conn;
|
||||
try {
|
||||
const result = await db.pool.query("delete from tasks where id = ?", [id]);
|
||||
res.send(result);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) return conn.release();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => console.log(`Listening on port ${port}`));
|
||||
app.listen(port, () => console.log(`Listening on port ${port}`));
|
||||
|
Reference in New Issue
Block a user