创建一个使用Node Express + SQL的按名称搜索功能。

huangapple go评论70阅读模式
英文:

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:
创建一个使用Node Express + SQL的按名称搜索功能。

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)
            }

        })
    })
})

huangapple
  • 本文由 发表于 2023年6月12日 19:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定