HTML.连接数据库
各位老大。下列的代码我想连接数据库。拉出数据,按报表形式展现出来。哪里有问题啊?需要改进。麻烦帮忙改进以下,,谢谢了
HTML。js作为后端,。。。。。。
下面的是js代码。。
const express = require('express');
const app = express();
const port = 3000;
const Connection = require('tedious').Connection;
const Request = require('tedious').Request;
// 数据库连接配置
const config = {
server: '*********',
authentication: {
type: 'default',
options: {
userName: '*********',
password: '*********'
}
},
options: {
encrypt: false,
database: '*********'
}
};
// 创建数据库连接池(这里为了简单起见,我们直接创建一个连接,但在生产环境中应该使用连接池)
let connection;
function createConnection() {
return new Connection(config);
}
// API路由处理函数
app.get('/api/data', (req, res) => {
// 如果连接不存在或已关闭,则创建一个新的连接
if (!connection || connection.closed) {
connection = createConnection();
}
// 连接到数据库(如果尚未连接)并执行查询
connection.connect((err) => {
if (err) {
return res.status(500).send('Error connecting to database');
}
// 创建并执行SQL请求
const request = new Request("SELECT TOP 100 MB001, MB002, MB064 FROM INVMB;", (err, rowCount) => {
if (err) {
console.error(err);
return res.status(500).send('Error executing query');
}
let result = [];
// 监听row事件以收集查询结果
request.on('row', (columns) => {
const row = {};
columns.forEach((column) => {
row = column.value;
});
result.push(row);
});
// 在查询完成后发送响应
request.on('done', () => {
res.json(result);
connection.close(); // 关闭连接(注意:在生产环境中,您应该将连接返回到连接池中,而不是关闭它)
});
});
connection.execSql(request);
});
});
// 启动服务器
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
下面的是HTML代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>看板示例</title>
<link rel="stylesheet" href="111.css">
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<div class="dashboard">
<div class="dashboard-header">
<h1>我的看板</h1>
</div>
<input type="text" placeholder="请输入查询的数据" />
<button type="button">提交</button>
<div class="dashboard-content">
<table id="dashboard-table">
<thead>
<tr>
<th>项目名称</th>
<th>描述</th>
<th>描述2</th>
</tr>
</thead>
<tbody id="table-body">
<!-- 数据行将通过JavaScript动态添加 -->
</tbody>
</table>
</div>
</div>
<script>
fetch('/api/data')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
const tableBody = document.getElementById('table-body');
data.forEach(item => {
const row = document.createElement('tr');
const nameCell = document.createElement('td');
nameCell.textContent = item.MB001;
const descriptionCell1 = document.createElement('td');
descriptionCell.textContent = item.MB002;
const descriptionCell2 = document.createElement('td');
descriptionCell.textContent = item.MB064;
row.appendChild(nameCell);
row.appendChild(descriptionCell);
tableBody.appendChild(row);
});
})
.catch(error => console.error('Error fetching data:', error));
</script>
</body>
</html>
和我们公司的框架一样离谱,就是前台直接连数据库,那几个老员工都是前端出身,不会玩儿后台,苦了我们后来的员工啊。 SherlockProel 发表于 2024-11-4 10:01
和我们公司的框架一样离谱,就是前台直接连数据库,那几个老员工都是前端出身,不会玩儿后台,苦了我们后来 ...
牛皮,这js是服务器后台在处理,还是客户端直接加载?
不加密直接等同于直接送了,加密是不是也很痛苦?
震惊!!!!!!!!!!!!!!!!!
html ,还连数据库??
离不离谱吧,你把报错给看一下啊 const express = require('express');
const app = express();
const port = 3000;
const Connection = require('tedious').Connection;
const Request = require('tedious').Request;
// 使用环境变量或其他方式管理数据库连接信息
const config = {
server: ******,
authentication: {
type: 'default',
options: {
userName: ******,
password: *******
}
},
options: {
encrypt: false,
database: ******
}
};
let connection;
function createConnection() {
return new Connection(config);
}
app.get('/api/data', (req, res) => {
if (!connection || connection.closed) {
connection = createConnection();
}
connection.connect((err) => {
if (err) {
return res.status(500).send('Error connecting to database');
}
const request = new Request("SELECT TOP 100 MB001, MB002, MB064 FROM INVMB;", (err, rowCount) => {
if (err) {
console.error(err);
return res.status(500).send('Error executing query');
}
let result = [];
request.on('row', (columns) => {
const row = {};
columns.forEach((column) => {
row = column.value;
});
result.push(row);
});
request.on('done', () => {
res.json(result);
// 在生产环境中,将连接返回到连接池
});
});
connection.execSql(request);
});
});
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>看板示例</title>
<link rel="stylesheet" href="111.css">
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<div class="dashboard">
<div class="dashboard-header">
<h1>我的看板</h1>
</div>
<input type="text" placeholder="请输入查询的数据" />
<button type="button">提交</button>
<div class="dashboard-content">
<table id="dashboard-table">
<thead>
<tr>
<th>项目名称</th>
<th>描述</th>
<th>描述2</th>
</tr>
</thead>
<tbody id="table-body">
<!-- 数据行将通过JavaScript动态添加 -->
</tbody>
</table>
</div>
</div>
<script>
fetch('/api/data')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
const tableBody = document.getElementById('table-body');
data.forEach(item => {
const row = document.createElement('tr');
const nameCell = document.createElement('td');
nameCell.textContent = item.MB001;
const descriptionCell1 = document.createElement('td');
descriptionCell1.textContent = item.MB002;
const descriptionCell2 = document.createElement('td');
descriptionCell2.textContent = item.MB064;
row.appendChild(nameCell);
row.appendChild(descriptionCell1);
row.appendChild(descriptionCell2); // 修正了之前的错误
tableBody.appendChild(row);
});
})
.catch(error => {
console.error('Error fetching data:', error);
// 可以在这里添加用户错误提示
});
</script>
</body>
</html>
// 使用环境变量或其他方式管理数据库连接信息
const config = {
server: process.env.DB_SERVER,
authentication: {
type: 'default',
options: {
userName: process.env.DB_USER,
password: process.env.DB_PASSWORD
}
},
options: {
encrypt: false,
database: process.env.DB_NAME
}
};
let connection;
function createConnection() {
return new Connection(config);
}
app.get('/api/data', (req, res) => {
if (!connection || connection.closed) {
connection = createConnection();
}
connection.connect((err) => {
if (err) {
return res.status(500).send('Error connecting to database');
}
const request = new Request("SELECT TOP 100 MB001, MB002, MB064 FROM INVMB;", (err, rowCount) => {
if (err) {
console.error(err);
return res.status(500).send('Error executing query');
}
let result = [];
request.on('row', (columns) => {
const row = {};
columns.forEach((column) => {
row = column.value;
});
result.push(row);
});
request.on('done', () => {
res.json(result);
// 在生产环境中,将连接返回到连接池
});
});
connection.execSql(request);
});
});
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
}); 离谱,写个程序不安全吗 SherlockProel 发表于 2024-11-4 10:01
和我们公司的框架一样离谱,就是前台直接连数据库,那几个老员工都是前端出身,不会玩儿后台,苦了我们后来 ...
严格来说,这不是前台直接连接数据库。这也是前后端分离啊,只是后端语言用的nodejs写的,而不是java的spring boot框架。 报错信息是什么?node服务端连上了吗 HTML连接数据库?你再怎么加密也有风险啊? d199212 发表于 2024-11-4 10:21
报错信息是什么?node服务端连上了吗
html .......404,,,,,,,,
可以正常连接。。。。。。