网站首页 > 基础教程 正文
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},{"a":2,"b":5,"c":6},{"a":1,"b":2,"c":3},{"a":1,"b":3,"c":6},{"a":1,"b":5,"c":6},{"a":2,"b":2,"c":3}]}
猜你喜欢
- 2024-12-02 GitHub精选 | 后台权限管理系统(基于Node.js)
- 2024-12-02 Node.js 的 5 个常见服务器漏洞
- 2024-12-02 Node-RED教程 – 22 modbus读取数据写入mysql并可查询显示曲线图
- 2024-12-02 MySQL 同步复制及高可用方案总结
- 2024-12-02 如何实现用prometheus+grafana实时监控mysql数据库?
- 2024-12-02 (完结无密) Vue3+Uni+Node+MySQL 从零实现跨端小程序的全栈应用
- 2024-12-02 Mysql-cluster搭建
- 2024-12-02 Mysql高可用方案--PXC
- 2024-12-02 Vue整合Node.js,直连Mysql数据库进行CURD操作
- 2024-12-02 MySQL数据库之Haproxy+PXC集群搭建(中)
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)