以下代码属于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');
}
});
您的代码中至少有两个问题,我可以看到
- 正如@AlwaysLearning已经提到的,您的sql配置对象被定义为
sqlConfig
,但您使用sql.ConnectionPool(config)
- 当调用
/admins
端点时,您的代码基本上什么都不做(除了res.render("./admin/masters", { title: "ADMIN"});
之外,您正在用async ()=>{...}
定义一个从未实际执行过的匿名函数。要执行匿名函数,您可以使用IIFE。在这种情况下,这不是一个好主意,因为会出现异步调用的问题。我怀疑您这样做是为了使用async/await。要实现这一点,您不需要将相关代码封装在匿名异步函数中on,但使整个回调函数异步
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', async function(req, res) {
try {
const pool = await sql.ConnectionPool(sqlConfig);
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");
}
});
我自己无法测试,但我希望它能有所帮助。
次要添加:
- 您可能想要在finally子句中关闭sql连接
- 在每次请求后最好不要关闭连接。但我不确定,因为我对mssql不太熟悉
-
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;