简体   繁体   English

如何在 vue.js 前端使用 Node.js 建立 MSSQL 连接?

[英]How to make MSSQL connection using Node.js in vue.js frontend?

I need to connect Vue.js app to the remote MSSQL server.我需要将 Vue.js 应用程序连接到远程 MSSQL 服务器。

I am using node.js, mssql library .我正在使用 node.js、 mssql 库

After setting up I get error.设置后我得到错误。

How to make configuration settings in order to connect to mssql?如何进行配置设置以连接到 mssql? How to solve issues related to installation of libraries?如何解决与安装库相关的问题?

    ERROR  Failed to compile with 5 errors                                                        1:39:06 PM

    These dependencies were not found:

    * dgram in ./node_modules/native-dns-cache/lookup.js, ./node_modules/native-dns/lib/server.js and 2 others
    * fs in ./node_modules/native-dns/lib/platform.js

    To install them, you can run: npm install --save dgram fs

package.json dependencies: package.json依赖:

  ...
  },
  "dependencies": {
    "dgram": "^1.0.1",
    "dns": "^0.2.2",
    "firebase": "^4.10.0",
    "fs": "0.0.1-security",
    "highcharts": "^6.0.7",
    "mssql": "^4.1.0",
    "net": "^1.0.2",
    "tls": "0.0.1",
    "vue": "^2.5.3",
    "vue-highcharts": "0.0.10",
    "vue-router": "^3.0.1",
    "vuetify": "^0.17.7",
    "vuex": "^3.0.1"
  }, ...

I solve this problem.我解决了这个问题。 As I am very new in this environment.因为我在这个环境中很新。 Vue app renders client-side. Vue 应用程序呈现客户端。 I build server-side using template " webpack-ssr ".我使用模板“ webpack-ssr ”构建服务器端。 Here is more information about server-side rendering这是有关服务器端渲染的更多信息

here is my test code:这是我的测试代码:

...

const sql = require('mssql')

...

// Create connection to database
const databaseConfig = {
  userName: 'USER_NAME',
  password: 'PASSWORD',
  server: 'SERVER_NAMECLOUDAPP.AZURE.COM',
  database: 'DATABASE',
  options: {
       encrypt: true // Use this if you're on Windows Azure
   }
}

...

Data conncetion:数据连接:

// mssql connect for database 

sql.connect(databaseConfig, err => {

    // ... error checks

if (err) {
  console.log('error: ' + err)

} else {
  console.log('conencted')
}
// Query

new sql.Request().query('select 1 as number', (err , result) => {
    // ... error checks

       ...

    console.dir(result + ' hmmm ... not good')
})
})

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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