NodeJS使用protobuffer的簡單示例

2021-10-07 08:00:20 字數 1015 閱讀 1540

1. 先定義乙個.proto的檔案,檔名稱, test.proto

message column
2. 使用nodejs 完成對資料的編碼及解碼

const fs = require('fs')

const protobuf = require('protocol-buffers');

// nodejs 使用protocol-buffers 這個庫進行編譯碼的操作

// 載入協議檔案中的模型

const schema = protobuf(fs.readfilesync(__dirname + '/test.proto', 'utf-8'));

// schema裡面有兩個函式,乙個是編碼的函式,乙個是解碼的函式

// encode: [function: encode] ,

// decode: [function: decode] ,

console.log(schema);

// 輸出結果

messages ,

decode: [function: decode] ,

encodinglength: [function: encodinglength],

dependencies: [ [object], [object], [object] ]

}}// using function about package protobuf

// 將資料根據模型,也就是.proto協議中定義的column,傳入並使用encode函式生成buffer

const buffer = schema.column.encode()

console.log(buffer);

// 輸出結果

// 使用decode將buffer的二進位制資料解析並列印出來

console.log(schema.column.decode(buffer));

// 輸出結果

Mac下Go安裝配置並使用Protobuf

首先使用home brew安裝protobuf brew install protobuf 安裝好之後,檢視是否安裝成功 protoc version 如果成功會有如下類似的版本號顯示 libprotoc 3.1.0 這表明我安裝成功,並且版本號是3.1.0。接著安裝protobuf的golang外...

Nodejs教程22 使用Nodejs運算元據庫

資料庫示例 lesson26 test.sql nodejs運算元據庫需要用到mysql模組,通過npm i mysql d進行安裝。之後可以通過mysql.createconnection方法新建乙個資料庫連線,需要傳入的引數有位址 埠 登入名 密碼,以及需要連線的資料庫。mysql.create...

nodejs使用mysql例子

傳送html響應 exports.sendhtml function res,html 解析post資料 exports.parsereceiveddata function req,cb req.on end function 渲染簡單的表單 exports.actionform function...