1.背景:
node.js是一款流行的运行在服务端的 JavaScript,使用JavaScript语言进行编程,快速实现一些简单web服务等功能。
2.连接GBase代码示例:
使用node.js前先进行环境安装,化简安装步骤如下:
先从node.js网站上下载node.js安装包,本例中安装包为,也可以从https://nodejs.org/zh-cn/download/prebuilt-binaries地址上手动下载
wget https://nodejs.org/dist/v22.11.0/node-v22.11.0-linux-x64.tar.xz // 下载
tar -xvf node-v10.9.0-linux-x64.tar.xz // 解压
cd node-v10.9.0-linux-x64/ // 进入解压目录
./bin/node -v // 执行node命令 查看版本
解压文件的 bin 目录底下包含了 node、npm 等命令,我们可以使用 ln 命令来设置软连接:
ln -s /usr/software/nodejs/bin/npm /usr/local/bin/
ln -s /usr/software/nodejs/bin/node /usr/local/bin/
以下为一段node.js连接gbase8a的代码示例,注意由于目前8a还么有javascript的连接驱动程序,因此需要使用mysql的连接驱动package来进行连接,本例中使用了express和mysql这两个package,需要提前进行安装,具体安装指令示例如下:
创建代码目录:
mkdir test
cd test
npm install express --save //下载方式安装express package
npm install mysql --save //下载方式安装mysql package
完成后会在创建的test目录下生成node_modules目录内部包括下载的全部package和其依赖package,并生成package.json,package-lock.json两个文件作为下载package的描述信息
以下为连接gbase8a进行简单查询的示例代码:
其中gbase8a中的test.t1表结构如下:
CREATE TABLE "t1" (
"a" int(11) DEFAULT NULL,
"b" int(11) DEFAULT NULL,
"c" int(11) DEFAULT NULL
) ENGINE=EXPRESS DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace'
具体node.js代码test.js文件内容如下:
const express = require('express');
const mysql = require('mysql');
const util = require('util');
const bodyParser = require('body-parser');
// Promisify the mysql query method for a given connection
const promisifyQuery = (connection) => {
const query = util.promisify(connection.query).bind(connection);
return query;
};
const app = express();
const myport = 3000;
app.use(bodyParser.json());
app.post('/connect', async (req, res) => {
const { host, user, password, database, port } = req.body;
if (!host || !user || !password || !database || !port) {
return res.status(400).json({ error: 'Missing required connection parameters' });
}
const connectionConfig = {
host: host,
user: user,
password: password,
database: database,
port: port
};
let connection;
try {
connection = mysql.createConnection(connectionConfig);
// Execute the SQL query
connection.query('SELECT a, b, c FROM t1 limit 10', function (error, results, fields) {
if(error){
console.log('[SELECT ERROR] - ',error.message);
return;
}
console.log('--------------------------SELECT----------------------------');
console.log(results);
console.log('------------------------------------------------------------\n\n');
res.json({results});
});
} catch (error) {
// Handle database connection or query errors
console.error('Database error:', error);
res.status(500).json({ error: 'Database connection or query failed', message: error.message });
} finally {
// Ensure the database connection is closed
if (connection) {
connection.end();
}
}
});
app.listen(myport, () => {
console.log(Server is running on http://localhost:${myport}
);
});
3.调用和执行效果:
运行test.js javascript
[gbase@kylin10danji test]$ node test1.js
Server is running on http://localhost:3000
使用curl工具发送post请求给服务端口:
[gbase@kylin10danji gbase]$ curl -X POST http://localhost:3000/connect -H "Content-Type: application/json" -d '{"host":"192.168.56.161","user":"gbase","password":"gbase20110531","database":"test","port":"5258"}'
test.js服务端打屏显示:
[gbase@kylin10danji test]$ node test1.js
Server is running on http://localhost:3000
--------------------------SELECT----------------------------
[
RowDataPacket { a: 1, b: 2, c: 3 },
RowDataPacket { a: 1, b: 3, c: 6 },
RowDataPacket { a: 1, b: 5, c: 6 },
RowDataPacket { a: 2, b: 2, c: 3 },
RowDataPacket { a: 2, b: 3, c: 6 },
RowDataPacket { a: 2, b: 5, c: 6 },
RowDataPacket { a: 1, b: 2, c: 3 },
RowDataPacket { a: 1, b: 3, c: 6 },
RowDataPacket { a: 1, b: 5, c: 6 },
RowDataPacket { a: 2, b: 2, c: 3 }
]
返回curl客户端信息:
[gbase@kylin10danji gbase]$ curl -X POST http://localhost:3000/connect -H "Content-Type: application/json" -d '{"host":"192.168.56.161","user":"gbase","password":"gbase20110531","database":"test","port":"5258"}'
{"results":[{"a":1,"b":2,"c":3},{"a":1,"b":3,"c":6},{"a":1,"b":5,"c":6},{"a":2,"b":2,"c":3},{"a":2,"b":3,"c":6},{"