将我的节点js程序连接到sql server之后,出现以下错误:
“消息”:“连接丢失-套接字挂断”, “ code”:“ ESOCKET”
我应该在节点js代码中添加或更改什么?
我的nodejs代码如下:
var express = require("express");
var bodyParser = require("body-parser");
var sql = require("mssql");
var app = express();
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,
contentType,Content-Type, Accept, Authorization");
next();
});
var server = app.listen(process.env.PORT || 1433, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
var dbConfig = {
user: "sa",
password: "Chetu@123",
server: "localhost",
database:"sample"
};
var executeQuery = function(res, query){
sql.connect(dbConfig, function (err) {
if (err) {
console.log("Error while connecting database :- " + err);
res.send(err);//this line is running
}
else {
var request = new sql.Request();
// query to the database
request.query(query, function (err, res) {
if (err) {
console.log("Error while querying database :- " + err);
res.send(err);
}
else {
res.send(res);
}
});
}
});
}
//GET API
app.get("/api/user", function(req , res){
var query = "select * from [Student]";
executeQuery (res, query);
});