node.js - 当我的 nodejs 应用程序尝试与 SQL Server 连接时,控制台上没有显示任何内容,即使没有错误
问题描述
下面的代码属于index.js
文件。
当我根据代码转到链接“localhost:300/admins/”时,它应该与 SQL Server 连接并在控制台上取回结果。
我的 Microsoft SQL Server Management Studio 2012 运行良好,并且从 Visual Studio 可以顺利运行。
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const hbs = require('hbs');
const sql = require('mssql');
const sqlConfig = {
user: 'xxx',
password: 'xxxx',
database: 'xxxxx',
server: '127.0.0.1',
port: xxxx,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
};
// static file's path set up
app.use(express.static('public'));
app.use('/css', express.static(__dirname + '/public'));
app.use('/images', express.static(__dirname + '/public'));
app.use('/js', express.static(__dirname + '/public'));
const pagespath = path.join(__dirname, './templates/views');
const partialspath = path.join(__dirname, './templates/partials');
// Set Views and view engine
app.set('view engine', 'hbs');
app.set('views', pagespath);
hbs.registerPartials(partialspath);
app.get('/', (req, res) => {
res.render('home', { title: 'Home' });
});
app.get('/admins', function (req, res) {
var result;
async () => {
try {
const pool = await sql.ConnectionPool(config);
const result = await pool.query`select name from tbl_info_p_admin`;
res._write(result);
console.log(result);
} catch (err) {
console.log(err);
}
await sql.close();
};
res.render('./admin/masters', { title: 'ADMIN' });
});
app.listen(process.env.PORT || 3000, function (err) {
if (err) {
console.log(err);
} else {
console.log('Server Started At Port 3000');
}
});
解决方案
MSSQL 服务器连接配置。
const config = { user: 'XX', // sql user password: 'xxxxxx', //sql user password server: '127.0.0.1', // if it does not work try- localhost database: 'xxxxxx', options: { trustServerCertificate: true, useColumnNames: true, rowCollectionOnDone: true, trustedconnection: true, enableArithAbort: false, instancename: 'MSSQLSERVER', // SQL Server instance name cryptoCredentialsDetails: { minVersion: 'TLSv1' }, }, port: 1433 } module.exports = config;
推荐阅读
- wpf - 如何从其用户控件内部关闭 WPF 中的弹出窗口
- session - 如何控制工作区会话超时消息 (ORACLE APEX 20.1)
- local - Helm 无法找到本地图表来更新依赖项
- xml - 在 XSLT / XPath 中 not(*|@*) 是什么意思?
- javascript - 如何使 d3-lasso 在 d3 forcedirected 网络上工作?
- github-actions - 在 github 工作流中运行超时命令
- arrays - 这种方法如何从数组中产生最高值?
- command - ./worldserver 和 ./authserver 不工作。找不到相关命令
- python - 让 POST 请求像 GET 请求一样工作?djangorestframework-datatables
- c++ - C++ 中的模块化