indexjsfunction getblogsInfo axios url httplocalhost3000apiblogsinfo method get thenresults = const bookList = resultsdatadata; const htmlStr = bookListmapitem inde
To make the code able to query and return HTML, you can modify the getSearchBlogs function in blogs.js as follows:
exports.getSearchBlogs = (req, res) => {
const info = req.query;
const id = `%${info.id}%`;
const title = `%${info.title}%`;
const author = `%${info.author}%`;
const classify = `%${info.classify}%`;
const sqlStr = `SELECT * FROM blogs_info WHERE id LIKE ? OR title LIKE ? OR author LIKE ? OR classify LIKE ?`;
db.query(sqlStr, [id, title, author, classify], (err, results) => {
if (err) return res.cc(err);
if (results.length === 0) return res.send('<h1>No matching blogs found.</h1>');
const htmlStr = results.map((item, index) => {
return `<tr>
<td>${index + 1}</td>
<td>${item.title}</td>
<td>${item.author}</td>
<td>${item.classify}</td>
<td>
<input type="button" value="删除" onclick="del(${item.id})">
</td>
</tr>`;
}).join('');
const finalHtml = `<table>${htmlStr}</table>`;
res.send(finalHtml);
});
};
In the above code, the info object is retrieved from the query parameters using req.query. The SQL query is modified accordingly to use LIKE operator for pattern matching. If no matching blogs are found, a simple HTML message is returned. Otherwise, the HTML table is constructed using the query results and sent as the response
原文地址: https://www.cveoy.top/t/topic/h2ER 著作权归作者所有。请勿转载和采集!