嘿,我在这里制作api是源代码
//Initiallising node modules
var express = require("express");
var bodyParser = require("body-parser");
var sql = require("mssql");
var app = express();
// Body Parser Middleware
app.use(bodyParser.json());
//CORS Middleware
app.use(function (req, res, next) {
//Enabling CORS
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();
});
//Setting up server
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
//Initiallising connection string
var dbConfig = {
user: 'sa',
password: '*****',
server: '94.200.123.222,1433',
database: 'CUBES_HO',
};
//Function to connect to database and execute query
var executeQuery = function(res, query){
sql.connect(dbConfig, function (err) {
if (err) {
console.log("Error while connecting database :- " + err);
res.send(err);
}
else {
// create Request object
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);
sql.close();
}
else {
res.send(res);
}
sql.close();
});
}
});
}
//GET API
app.get("/api/user", function(req , res){
var query = "select * from [osama] where bill_hdr_key = '31'";
executeQuery (res, query);
});
问题是,当我执行此操作时,它会给我错误 无法在94.200.123.222,1433上查找实例 - getaddrinfo ENOTFOUND 94.200.123.222,1433", 服务器已启动并正在运行,可通过SSMS进行访问。亲切地帮助我
答案 0 :(得分:0)
尊重您的代码,我试图通过以下代码解决您的问题,您犯的错误很少,所以我在这里纠正了它。
const express = require("express");
const bodyParser = require("body-parser");
const sql = require("mssql");
const app = express();
const port = 3131;
// Body Parser Middleware
app.use(bodyParser.json());
//CORS Middleware
app.use(function (req, res, next) {
//Enabling CORS
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();
});
//Initiallising connection string
var dbConfig = {
user: 'sa',
password: "******",
server: '94.200.123.222',
database: 'CUBES_HO',
};
function executeQuery(query) {
return new Promise((resolve, reject) => {
sql.connect(dbConfig, function (err) {
if (err) {
reject(err);
sql.close();
} else {
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query(query, function (err, data) {
if (err) {
reject(err);
sql.close();
} else {
resolve(data);
sql.close();
}
});
}
});
});
}
//GET API
app.get("/", function (req, res) {
var query = "select * from [osama] where bill_hdr_key = '31'";
executeQuery(query)
.then((data) => {
res.status(200).send({ "msg": "Records fetched", "data": data.recordsets });
}).catch((err) => {
res.status(500).json({ "msg": err.message });
});
});
//Setting up server
var server = app.listen(port || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
希望,它可能有助于解决您的错误。