Oracle 与 NodeJS 和 ExpressJS 的连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23653600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:21:34  来源:igfitidea点击:

Oracle connectivity with NodeJS and ExpressJS

node.jsoraclenpm

提问by Ayyappa A

I am trying to connect oracle with NodeJS. Steps followed as below. Pre - requisites there is no client installed except NodeJS(0.10.25) and npm(1.3.24).

我正在尝试将 oracle 与 NodeJS 连接。步骤如下。先决条件 除了 NodeJS(0.10.25) 和 npm(1.3.24) 之外,没有安装客户端。

In my ExpressJS Project:

在我的 ExpressJS 项目中:

  • npm install db-oracle
    There is no error found while installing.

  • In app.js

    var oracle = require('db-oracle');
    var connString = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxxx)))";
    var connectData = { "tns": connString, "user": "XPPS_OWNER", "password": "Ex3ec" };
    
  • In one of the endpoint i coded like this,

    app.get('/oracleendpoint',function(req,res){
    connectData.connect(function(error) {
      if (error) {
        return console.log("CONNECTION ERROR: " + error);
      }
    
    this.query().select('*').from('Users').execute(function(error, rows) {
        if (error) {
            return console.log('ERROR: ' + error);
        }
        console.log(rows.length + ' ROWS');
       });
    });
    });
    
  • npm install db-oracle 安装时
    没有发现错误。

  • 在 app.js 中

    var oracle = require('db-oracle');
    var connString = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=xxxx)))";
    var connectData = { "tns": connString, "user": "XPPS_OWNER", "password": "Ex3ec" };
    
  • 在我这样编码的端点之一中,

    app.get('/oracleendpoint',function(req,res){
    connectData.connect(function(error) {
      if (error) {
        return console.log("CONNECTION ERROR: " + error);
      }
    
    this.query().select('*').from('Users').execute(function(error, rows) {
        if (error) {
            return console.log('ERROR: ' + error);
        }
        console.log(rows.length + ' ROWS');
       });
    });
    });
    

Expected output is to connect the DB.

预期输出是连接数据库。

FYI: DB Instance is running fine.

仅供参考:数据库实例运行良好。

Query:

询问:

  • What is the DB Name?
  • It is showing an error which show below. what would be root cause for this?

    module.js:340
    throw err;
        ^
    Error: Cannot find module './build/Release/oracle_bindings'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> 
    (C:\xampp\htdocs\mytest\node_modules\db-oracle\db-oracle.js:18:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    
  • 什么是数据库名称?
  • 它显示一个错误,如下所示。造成这种情况的根本原因是什么?

    module.js:340
    throw err;
        ^
    Error: Cannot find module './build/Release/oracle_bindings'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> 
    (C:\xampp\htdocs\mytest\node_modules\db-oracle\db-oracle.js:18:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    

Please let me know any missing or Installation required or any node modules required or code changes. I have Googled but in vain. Haven't found proper document for this error.

请让我知道任何缺失或需要安装或需要任何节点模块或代码更改。我已经谷歌搜索但徒劳无功。尚未找到针对此错误的正确文档。

回答by Matt M.

Oracle themselves have just released an Oracle driver for node.js: https://blogs.oracle.com/opal/entry/introducing_node_oracledb_a_node

Oracle 自己刚刚发布了一个用于 node.js 的 Oracle 驱动程序:https: //blogs.oracle.com/opal/entry/introducing_node_oracledb_a_node

Looks promising.

看起来很有希望。

回答by Andrei Karpushonak

In your example, you use db-oraclemodule, I would recommend to use node-oraclemodule instead, as it is more popular/better maintained.

在您的示例中,您使用db-oracle模块,我建议改用node-oracle模块,因为它更受欢迎/维护得更好。

To connect to Oracle DB from Node, you have to follow the instructions on the page mentioned above.

要从 Node 连接到 Oracle DB,您必须按照上述页面上的说明进行操作。