英文:
Make a search function by name using Node Express + SQL
问题
以下是您提供的代码的翻译部分:
// 搜索啤酒
app.get('/search/:query', (req, res) => {
pool.getConnection((err, connection) => {
if(err) throw err
console.log(`连接成功,ID为 ${connection.threadId}`)
search = req.query.search
connection.query("SELECT * FROM beers WHERE name LIKE ?", [`%${search}%`], (err, rows) => {
connection.release() // 将连接返回到连接池
if(!err) {
res.send(rows)
} else {
console.log(err)
}
})
})
})
这是您的代码的翻译部分。如果您需要任何进一步的帮助,请随时提出。
英文:
So i wanted to make an API with a search function with SQL queries using node express, this is what my code looked like:
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)
}
})
})
})
And this is how my postman looked:
The SQL query is correct but I dont know how to make the search routing in Node
答案1
得分: 1
你可以尝试这样做
app.get('/search', async function(req, res) {
let page = req.query.page;
let limit = req.query.limit;
let search = req.query.q;
//
pool.getConnection((err, connection) => {
if (err) throw err
console.log(`connected as id ${connection.threadId}`)
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)
}
})
})
});
并且像这样调用API http://localhost:5000/search?q=yoursearchterm,还要检查可能的SQL注入问题。
英文:
You can try this
app.get('/search', async function(req, res) {
let page = req.query.page;
let limit = req.query.limit;
let search= req.query.q;
//
pool.getConnection((err, connection) => {
if(err) throw err
console.log(`connected as id ${connection.threadId}`)
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)
}
})
})
});
and call api like this http://localhost:5000/search?q=yoursearchterm, also check for possible sql injection issue.
答案2
得分: 1
你可以通过以下方式从路径变量中获取参数:
req.params.query
这样你的代码会像这样:
app.get('/search/:query', (req, res) => {
pool.getConnection((err, connection) => {
if (err) throw err
console.log(`connected as id ${connection.threadId}`)
search = req.params.query
connection.query("SELECT * FROM beers WHERE name LIKE ?", [`%${search}%`], (err, rows) => {
connection.release() // 将连接返回到池中
if (!err) {
res.send(rows)
} else {
console.log(err)
}
})
})
})
英文:
You can get the parameter from path variables by this
req.params.query
so your code will be like this
app.get('/search/:query', (req, res) => {
pool.getConnection((err, connection) => {
if(err) throw err
console.log(`connected as id ${connection.threadId}`)
search = req.params.query
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)
}
})
})
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论