我正在使用express4 4乏味的方法来构建一个SQL
这是我的密码
const express = require('express'); // OAuth2 needs a web server to call back to
const morgan = require('morgan'); // morgan is HTTP middleware and logging
const path = require('path'); // helps resolving file paths
const fs = require('fs'); // filesystem
// https://www.npmjs.com/package/express4-tedious
const tediousExpress = require('express4-tedious');
dbconfig=
{
"server" : "localhost",
"userName": "uname",
"password": "pwd",
"options": { "encrypt": true, "database": "MyDB", "trustServerCertificate":false }
}
const app = express();
// add tediousExpress to the middleware chain
app.use(function (req, res, next) {
req.sql = tediousExpress(dbconfig);
next();
});
// Initialize variables.
const port = 3030;
// Configure morgan module to log all requests.
app.use(morgan('dev'));
/* commented out to simplify issue
// set up route for static files
app.use(express.static('webapp/static'));
app.use(express.static('webapp/js'));
app.use(express.static('webapp/library'));
*/
// Start the server.
app.listen(port);
console.log('Listening on port ' + port + '...');
// Set up a route for index.html.
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/webapp/index.html'));
});
app.get('/sm', function (req, res) {
console.log("handling system")
res.setHeader("Content-Type", "application/json");
req
.sql("select * from Mytable for json path")
.fail(function(ex, res) {
res.statusCode = 500;
res.write(ex.message);
res.end();
} )
.into(res, '{}');
// This always shows []
console.log(res.outputData);
// B. If I don't put this line in, the web page gets stuck
res.send(res.outputData);
});我正在用VS代码来运行这个。控制台在我点击URL时显示了这一点
handling system
[]代码注释中提到了我的问题。
问题A
res.outputData总是显示[],即使我知道查询返回JSON。
问题B
如果我不把res.send(res.outputData);放在代码中,当我调用url时
另外,
我对node.js还不太熟悉,无法解决问题。我觉得模块里可能有什么不对劲的地方,没有给我看。
当我在VS代码中执行和调试时,我可以看到一个关于未找到的页面的错误,但是我认为这可能是一个红色的鲱鱼。
正如我所了解的,这个代码是80%的剪切‘n’粘贴。
如果有一个比express4-tedious更可靠的SQL库,我很乐意接受建议。
这里有示例代码https://github.com/JocaPC/express4-tedious
编辑:
发布于 2022-06-28 20:07:38
你的
dbconfig=
{
"server" : "localhost",
"userName": "uname",
"password": "pwd",
"options": { "encrypt": true, "database": "MyDB", "trustServerCertificate":false }
}be
dbconfig=
{
server : "localhost",
userName: "uname",
password: "pwd",
options: { encrypt: true, database: "MyDB", trustServerCertificate:false }
}https://stackoverflow.com/questions/71738926
复制相似问题