从Node.js连接Oracle [英] Connect oracle from nodejs
问题描述
最近几周,我一直在尝试从我的nodejs代码连接oracle db.到目前为止,我发现了2个主要库,例如 https://github.com/mariano/node- db-oracle ,它已过期(最近一次更新是在一年前),第二个是 https: //github.com/nearinfinity/node-oracle 确实是最新的,但是我没有设法将oracle与这些模块中的任何一个连接.
Last few weeks I had been trying to connect oracle db from my nodejs code. What I found so far are 2 main libraries such as https://github.com/mariano/node-db-oracle which is out of date (last update was year ago) and second one is https://github.com/nearinfinity/node-oracle which is really up to date, however I didn't manage to connect oracle with any of those modules.
市长的问题是npm install oracle//pr db-oracle由于
Mayor issue that npm install oracle //pr db-oracle fails due to
../src/connection.h:10:18: fatal error: occi.h: No such file or directory
我试图克隆代码并执行本地安装,然后将整个模块复制到我的项目下,安装好,但是当我将模块放在项目下时,我遇到了此错误
I tried to clone the code and perform local install and then copy entire module under my project, install wen well, but when I place module under my project I run into this error
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> (/var/www/node-test/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)
我一直遵循两个驱动程序的安装过程,并且已经设置了变量 像这样(/var/environment)
I had been follow installation procedure for both drivers and had been setup variables like this (/var/environment)
OCI_HOME=/opt/instantclient_12_1
OCI_VERSION=12
OCI_INCLUDE_DIR=/opt/instantclient_12_1/sdk/include
OCI_LIB_DIR=/opt/instantclient_12_1
LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client/lib
我使用的是ubuntu 12.04,节点版本为v0.10.18 这是我的示例nodejs测试文件:
I used ubuntu 12.04, and node version is v0.10.18 here is my sample nodejs test file:
var oracle = require('oracle');
new oracle.Database({
hostname: 'myserver.com',
port: 1521,
user: 'myuser',
password: 'mypass',
database: 'XE'
}).connect(function(error) {
if (error) {
return console.log("CONNECTION ERROR: " + error);
}
this.query("select * FROM `store`").execute(function(error, rows) {
if (error) {
return console.log('ERROR: ' + error);
}
console.log(rows.length + ' ROWS');
});
});
任何其他提示都会很好.我尝试了noradle( https://github.com/kaven276/noradle ),看起来好像很重为了我的目的.
any additional hint would be nice. I tried noradle (https://github.com/kaven276/noradle) that one seems to be to heavy for my purpose.
推荐答案
我知道这是一篇老文章...只是想提及一种确定的方式,使nodejs无需额外的模块即可与oracle通信.
I know this is an old post... just wanted to mention a sure way for nodejs to communicate to oracle without extra modules.
设置oracle,以便它可以创建和接收http请求.有几种方法可以做到这一点:
Set up oracle so it can create and receive http requests. There are a few ways to do this:
最简单的方法是打开epg网关:
The easiest is to turn on the epg gateway:
您还可以设置modplsq:
Also you can setup modplsq:
或Apex侦听器:
然后在node js中执行标准的http.get:
Then in node js do a standard http.get:
http.get("http://localhost/accessor/myschema.my_procedure?x=1&y=2", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
采用任何一种方法...使用安全的oracle,以便它仅响应nodejs服务器的IP地址.因此,如果在localhost上运行:
Whichever approach...secure oracle so that it will only respond to the ip address of the nodejs server. So if running on localhost:
if owa_util.get_cgi_env('REMOTE_ADDR') = '127.0.0.1' then
--ok
else
-- fail
end if;
还阻止对其他所有程序包和过程的调用.有几种方法可以执行此操作,具体取决于您选择的路径.
Also block calls to every other package and procedure. There are few ways to do this depending on the path you take.
请确保至少执行此操作:
Make sure you do this at a minimum:
- 创建可在网上调用的项的白名单
- 要求所有url都包含架构名称,例如:myuser.myprocedure
- 确保网址的第一部分(直至查询路径)仅包含a-z 0-9.
- 一个很好的白名单确实可以解决所有这些问题
您已经拥有了它...无需担心模块是否会在下一版本中中断或停止工作.
There you have it...no need to worry if a module will break or stop working with the next release.
并且...您可以轻松地从Oracle交流使用Node:
AND...you can easily communicate from Oracle to Node use:
- apex_web_service.make_rest_request
- utl_http
这篇关于从Node.js连接Oracle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!