我使用带有mssql
包 ( https://www.npmjs.com/package/mssql ) 的 node express 应用程序从我的 Azure SQL Server 数据库中读取数据。一般来说,我可以读取数据库中的表格,但是,我不知道如何将表格可视化到我使用把手模板作为节点的首页。
这是我的代码:
路线/index.js
const express = require('express');
const router = express.Router();
const config = require('../dbconfig');
const sql = require('mssql');
// Get homepage
router.get('/', function(req, res) {
// Read data rows from the database (dbo.lendbook table)
new sql.ConnectionPool(config).connect().then(pool => {
return pool.query `select * from dbo.lendbook`
}).then(result => {
res.render('index');
// Output the data which was read in the terminal:
// console.dir(result);
}).catch(err => {
console.log(err);
});
视图/index.handlebars
<h2 class="page-header">Dashboard</h2>
<p>Welcome to your dashboard</p>
{{#each rows}}
<div>{{item}}</div>
{{/each}}
这是我可以通过上面的函数读取的数据库中的表