我正在开发一个通过 Tedious 请求 SQL Server 数据库的小型 Nodejs/Express 网站。
当我在任何功能之外创建连接时,它似乎可以工作,但我无法刷新。
当我尝试将它放在我的 connx 函数中时,没有任何附加内容,它似乎没有按conn.on('connect'...)
顺序输入。
我很迷茫。希望你能帮助我。
var http = require('http');
var jade = require('jade');
var moment = require('moment');
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var express = require('express');
var app = express();
var config = {
server: 'myserver',
userName: 'myuser',
password: 'mypassword',
options: {
instanceName: 'myinstance',
database: 'mydb',
}
};
app.set('views','./views');
app.set('view engine','jade');
app.use('/public',express.static(__dirname + '/public'));
var results = [];
function requete1(dated, datef) {
return ligne = "select Batch, Parametres, Etat, Durée, Resultat " +
"from LancementsNuit where HeureDebut >= '"+dated+" 18:00:00' " +
"and HeureFin <= '"+datef+" 18:00:00'";
}
function connx(x_date1,x_date2) {
var conn = new Connection(config);
//flush de result
result = [];
conn.on('connect', function () {
console.log(requete1(x_date1,x_date2));
var request = new Request(requete1(x_date1,x_date2), function(err, rowCount) {
if (err)
console.error(err);
});
request.on('row', function(row) {
results.push({
batch: row[0].value,
parametres: row[1].value,
etat: row[2].value,
duree: row[3].value,
resultat: row[4].value
})
});
conn.execSql(request);
conn.close();
})
}
app.get('/', function (req, res) {
var datef = moment().format('YYYY-MM-DD');
console.log(datef);
var Weekday = moment().isoWeekday();
if (Weekday == "1") {
var dated = moment(datef,'YYYY-MM-DD').subtract(3, 'd').format('YYYY-MM-DD');
} else {
var dated = moment(datef,'YYYY-MM-DD').subtract(1, 'd').format('YYYY-MM-DD');
}
connx(dated, datef);
console.log(results);
res.render('index', { data: results,
dated: dated,
datef: datef
});
})
app.get('/:dated', function (req, res) {
// console.log(results);
var datef = req.params.dated;
var Weekday = moment(datef,'YYYY-MM-DD').isoWeekday();
if (Weekday == "1") {
var dated = moment(datef,'YYYY-MM-DD').subtract(3, 'd').format('YYYY-MM-DD');
} else {
var dated = moment(datef,'YYYY-MM-DD').subtract(1, 'd').format('YYYY-MM-DD');
}
console.log("dated = " + dated + "; datef = " + datef);
connx(dated, datef);
console.log(results);
res.render('index', { data: results,
dated: dated,
datef: datef
});
})
app.listen(Number(process.argv[2]));