简体   繁体   中英

Connecting SQL Server with NodeJS app

I'm getting an error

Failed to look up instance on DESKTOP-N5***R/ONEJOHI

I'm trying to access my SQL Server instance using NodeJS. Here is my source code so far, I don't understand where I'm going wrong.

The app.js file requires mssql module and uses it to connect using the config object.

var express = require("express");
var bodyParser = require("body-parser");
var path = require("path");
var config = require('./bin/config');
const sql = require("mssql");

var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));

sql.connect(config).then(() => {
    return sql.query`select * from [dbo].[DimProduct]`;
}).then(res => {
    console.log(res);
}).catch(err => {
    console.log(err);
});

app.use("/", require("./router/index"));

app.listen(1000, () => {
    console.log("Server listening on port 1000");
});

The configuration in the config.js file is as follows.

const config = {
    user: 'DBA',
    password: 'Password01',
    server: "DESKTOP-N50AKSR/ONEJOHI",
    database: "FKLMasterData",
    port: 50862,
    options: {
        instanceName: "ONEJOHI"
    }
}

module.exports = config;

My Server instance is running and here is the PORT, I took a screenshot of it.

当前正在运行的端口?

Here are the SQL Server properties as shown in the properties window.

在此处输入图片说明

I don't understand why its not connecting. The last photo shows the error message as displayed by NodeJS every time I try to run the app.

在此处输入图片说明

Where could I be going wrong when trying to connect to the server?

I have made a connection with sql server. I have created below module and work for me.

var sql = require('mssql');
 var _ = require('underscore');


module.exports = function(exp){

var config = {
   server : process.env.DB_HOST + "\\" + process.env.DB_SERVER,
   database: process.env.DB_NAME,
   user: process.env.DB_USER,
   password: process.env.DB_PASSWORD,
   driver: "msnodesqlv8",
   options: {
      trustedConnection: false,
      encrypt: true // Use this if you're on Windows Azure
  },
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 30000
  },
 port: process.env.PORT
};

 var sqlConn = new sql.connect(config);
 exp.sqlConn=sqlConn; 

};

I believe you have to use some driver as "msnodesqlv8". You can install this driver with npm install msnodesqlv8 --save.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM