im try to learn how to use protobuff(protocol buffers) while sending it to web server that i create. i want to make a protobuff object, insert information inside it, serialize it, save it to file, send the file via http to web server, then deserialize it and print it to the console.
I created a simple web server in NodeJS express, built a protobuff object called employee & employees (list of employee)
syntax = "proto3";
message Employee {
int32 id = 1;
string name = 2;
float salary = 3;
}
message Employees {
repeated Employee employees = 1;
}
After using proto-c I created new program with some employee objects, filled them with some information, put them together in empoyees object and used the SerializeBinary function to save it as Array of bytes in an external file.
const ofir = new Schema.Employee();
ofir.setId(1001);
ofir.setName("Ofir");
ofir.setSalary(9000);
const dudu = new Schema.Employee();
dudu.setId(1002);
dudu.setName("Dudu");
dudu.setSalary(3000);
const rick = new Schema.Employee();
rick.setId(1003);
rick.setName("Rick");
rick.setSalary(5000);
const employees = new Schema.Employees();
employees.addEmployees(ofir);
employees.addEmployees(dudu);
employees.addEmployees(rick);
const bytes = employees.serializeBinary();
console.log("bytes type: " + typeof(bytes));
console.log("byte:");
console.log(bytes);
fs.writeFileSync("employeesbinary", bytes);
Now, I try to send this file (employeesbinary) to my webserver, receive it, deserialized its content and print it.
I use curl command:
>> curl --data-binary "@/Users/ofir.benezra/jsProj/employeesbinary" -X POST http://localhost:5004/proto
And that’s how I handle it in my webserver :
const express = require('express'); // Import Node.js core module
const Schema = require('./employees_pb');
const fs = require('fs');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
const port = 5004;
// POST method route
app.post('/proto', (req, res) => {
var check = Uint8Array.from(req.body);
const employees3 = Schema.Employees.deserializeBinary(req.body);
console.log(employees3.toString());
That’s the body of the message that I receive (req.body) :
(from debug breakpoint: )req.body= {\n�Ofir�F\n�Dudu�;E\n�Rick@�E: ''}
If I try to deserialized it (req.body) I receive error that say its not at the right format (Uint8Array):
AssertionError: Failure: Type not convertible to Uint8Array.
And If I try to use:
Uint8Array.from(req.body)
The result is empty array :
(from debug breakpoint: )check = Uint8Array(0) [buffer: ArrayBuffer(0), byteLength: 0, byteOffset: 0, length: 0]
And of course that’s the output after using Deserialized function :
“”
When I try to serialize, save in local file, open the file, deserialized its content and print it (with toString() method), somehow its work perfectly,
const bytes = employees.serializeBinary();
fs.writeFileSync("employeesbinary", bytes);
//stage 1 : binary convert
const employees2 = Schema.Employees.deserializeBinary(bytes);
console.log("employees: " + employees2.toString());
console.log("binary: " + bytes);
//stage 2 : file open
var a = fs.readFileSync('./employeesbinary');
console.log(a);
const employees3 = Schema.Employees.deserializeBinary(a);
console.log(employees3.toString());
OUTPUT:
/usr/local/bin/node ./webServer2.js
binary: 10,14,8,233,7,18,4,79,102,105,114,29,0,160,12,70,10,14,8,234,7,18,4,68,117,100,117,29,0,128,59,69,10,14,8,235,7,18,4,82,105,99,107,29,0,64,156,69
webServer2.js:30
Buffer(48) [10, 14, 8, 233, 7, 18, 4, 79, 102, 105, 114, 29, 0, 160, 12, 70, 10, 14, 8, 234, 7, 18, 4, 68, 117, 100, 117, 29, 0, 128, 59, 69, 10, 14, 8, 235, 7, 18, 4, 82, 105, 99, 107, 29, 0, 64, 156, 69, buffer: ArrayBuffer(8192), byteLength: 48, byteOffset: 1256, length: 48]
1001,Ofir,9000,1002,Dudu,3000,1003,Rick,5000
So the problem should be the way I handle the request on the server.
Thank you for your time.