![](/img/trans.png)
[英]NodeJs express4-tedious - Stored Procedure for SELECT to return response as JSON
[英]tedious4 express never returns response
我正在使用 express4-tedious 構建 SQL Web API
這是我的代碼
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 Code 中運行它。 當我點擊 URL 時,控制台會顯示這個
handling system
[]
代碼注釋中提到了我的問題
res.outputData 總是顯示 [] 即使我知道查詢返回 JSON。
如果我不放res.send(res.outputData);
在代碼中,當我調用 url
還,
我對 node.js 不夠熟悉,無法進行故障排除。 我覺得模塊內部可能有一些問題沒有顯示給我。
當我在 VS Code 中單步調試和調試時,我可以看到一個關於找不到頁面的奇怪錯誤,但我認為這可能是一個紅鯡魚。
據我所知,這段代碼是 80% 的剪切和粘貼。
如果有比express4-tedious
更可靠的 SQL Web API 庫,我很樂意接受建議。
這里有示例代碼https://github.com/JocaPC/express4-tedious
如果您的
dbconfig=
{
"server" : "localhost",
"userName": "uname",
"password": "pwd",
"options": { "encrypt": true, "database": "MyDB", "trustServerCertificate":false }
}
是
dbconfig=
{
server : "localhost",
userName: "uname",
password: "pwd",
options: { encrypt: true, database: "MyDB", trustServerCertificate:false }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.