You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NodeJS连接SQL Server 2019异常:连接后立即进入Final状态无法处理请求

注册页面SQL Server连接自动关闭问题求助

开发测试注册页面,已完成基础功能,本地NodeJS服务器启动后能连接SQL Server 2019,但连接成功后立刻进入Final状态并关闭连接,导致无法处理后续注册请求。

代码片段

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const port = 3000;

// Middleware
app.use(bodyParser.json());

var Connection = require('tedious').Connection;  
    var config = {  
        server: 'XXXXXXXXXXXXXXXXXX',  //update me
        authentication: {
            type: 'default',
            options: {
                trustServerCertificate: true 
            }
        },
        options: {
            encrypt: false,
            database: 'XXXXXXX',  //update me
            port: XXXXXXX
        }
    };  

    var connection = new Connection(config);  

    connection.connect(); 

    connection.on('connect', function(err) {  
        // If no error, then good to proceed.
        console.log("Connected"); 
        ExecuteFunction();

    });

    connection.on('debug', function(err) { console.log('debug:', err);});
    
    var Request = require('tedious').Request  
    var TYPES = require('tedious').TYPES; 

        app.get('/signup', (req, res) => {
            res.sendFile("XXXXXXXXXXXXXXXXXXXXX");
        });

        function ExecuteFunction() {        
        // Route to handle signup request
        app.post('/signup', (req, res) => {
            const { username, email, password } = req.body;

        var request = new Request('INSERT INTO Users (Username, Email, PasswordHash) VALUES (@username, @email, @password)', function(err) {  
         if (err) {  
            console.log("Error:", err);}  
        });  

        request.addParameter('Username', TYPES.VarChar, username);  
        request.addParameter('Email', TYPES.NVarChar, email);  
        request.addParameter('PasswordHash', TYPES.VarChar, password);  
        request.on('row', function(columns) {  
            columns.forEach(function(column) {  
              if (column.value === null) {  
                console.log('NULL');  
              } else {  
                console.log(" New Signup " + column.value);  
              }  
            });  
        });
        request.on("requestCompleted", function (rowCount, more) {
    });
        connection.execSql(request);  
    });
    }

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

预期流程

创建本地Node服务器并连接SQL Server,等待前端注册页面请求,收到请求后将数据写入SQL Server。

当前调试日志

Server running on port 3000
debug: connected to XXXXXXXXXXXXXXXX
debug: State change: Connecting -> SentPrelogin
debug: State change: SentPrelogin -> SentLogin7WithStandardLogin
Connected
debug: State change: SentLogin7WithStandardLogin -> Final
debug: connection to XXXXXXXXXXXXXXXX closed
debug: State is already Final

本人是SQL Server和NodeJS新手,不确定是否是SQL Server配置(如SentLogin7WithStandardLogin)存在问题,急需相关帮助!

内容的提问来源于stack exchange,提问作者Sam Brusky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 18:13:19