gpt4 book ai didi

javascript - 根据可选路线的不同查询 -expressjs

转载 作者:行者123 更新时间:2023-12-03 02:46:20 25 4
gpt4 key购买 nike

是否可以根据可选路线创建不同的查询?

app.get('/:genre/:book?', function (req, res) {

var genre = req.params.genre;
var book = req.params.book;

var sql = 'SELECT * FROM books WHERE genre = ? AND book = ?';

con.query(sql, [genre, book], (err, rows) => {
if (err) throw err
res.send(rows);
});

});

最佳答案

也许是这样的?

app.get('/:genre/:book?', function (req, res) {

var genre = req.params.genre;
var book = req.params.book;

var sql = 'SELECT * FROM books WHERE genre = ?';
var params = [genre];

if(book) {
sql += ' AND book = ?';
params = [genre, book];
}

con.query(sql, params, (err, rows) => {
if (err) throw err
res.send(rows);
});

});

关于javascript - 根据可选路线的不同查询 -expressjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48080464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com