簡體   English   中英

tedious4 express 從不返回響應

[英]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
[]

代碼注釋中提到了我的問題

問題A

res.outputData 總是顯示 [] 即使我知道查詢返回 JSON。

問題 B

如果我不放res.send(res.outputData); 在代碼中,當我調用 url

  • 網頁永遠旋轉
  • 我可以在 chrome 網絡監視器中看到我的 url 沒有響應

還,

  • 顯式發送 res.outputData 數據作為參數似乎是多余的
  • 這段代碼只是我從實驗中得出的
  • 以下鏈接中的示例代碼都不需要此附加行

堆棧溢出

github

我對 node.js 不夠熟悉,無法進行故障排除。 我覺得模塊內部可能有一些問題沒有顯示給我。

當我在 VS Code 中單步調試和調試時,我可以看到一個關於找不到頁面的奇怪錯誤,但我認為這可能是一個紅鯡魚。

據我所知,這段代碼是 80% 的剪切和粘貼。

如果有比express4-tedious更可靠的 SQL Web API 庫,我很樂意接受建議。

這里有示例代碼https://github.com/JocaPC/express4-tedious

編輯:

  • 我添加了一個 .fail 函數,但結果沒有變化
  • 我放了一個無意義的服務器名稱,它的行為相同。 所以看起來它失敗了,但沒有引發任何錯誤或提供任何線索

如果您的

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM