[英]How do I extract data from MySQL and display it on a webpage with Node.js?
我正在尝试从 MySQL 中提取数据并将其显示在我的 HTML 页面上这是我在 server.js 中的代码:
const path = require('path');
const express = require('express');
const bodyParser = require("body-parser");
const products = require("./utils/products.js");
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({extended:true}));
app.use("/products", products);
app.get('/', (req, res) => {
res.send("hei");
})
const PORT = 5000;
const HOST = '0.0.0.0';
app.listen(PORT, HOST,() => console.log(`Server running on ${HOST}:${PORT}`));
这是我在 HTML 中的代码:
<div class="col-lg-3 text-center">
<div class="card border-0 bg-light mb-2">
<div class="card-body">
<img src="vintage.jpg" class="img-fluid" alt="">
</div>
</div>
<h6>Vintage Two Piece Set</h6>
<p>$39.99</p>
<button>View Details</button>
</div>
我想从 MySQL 数据库中获取产品名称和价格,并将其显示在我的 html 页面上,如下图所示:产品数据
如何使用 Node.js 和快递来做到这一点。
所以基本上我想在运行我的 webproject 后立即获取 HTML 上的产品数据。
代表感谢!
您可以使用 ORM 更轻松地访问您的数据库(我喜欢在节点应用程序上使用typeORM
),并使用hbs
在您的应用程序上使用MVC
,因此您可以访问您的节点应用程序并在同一应用程序中显示数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.