After deciding to create an API with a search feature using SQL queries in node express, this is how I structured my code:
app.get('/search/:query', (req, res) => {
pool.getConnection((err, connection) => {
if(err) throw err
console.log(`connected as id ${connection.threadId}`)
search = req.query.search
connection.query("SELECT * FROM beers WHERE name LIKE '%${search}%' ", (err, rows) => {
connection.release() // return the connection to pool
if(!err) {
res.send(rows)
} else {
console.log(err)
}
})
})
})
When testing it on Postman, this is what my setup looked like: https://i.stack.imgur.com/wE1wm.png
The SQL query appears to be correct, but I am unsure how to properly implement the search routing in Node.