Help us understand the problem. What is going on with this article?

Node.js の gRPC で Redis のデータを作成 (Create)

設定ファイル、サーバープログラム、クライアントプログラムの3つが必要です。

設定ファイル

redis_create.proto
こちらと同じ
Python の gRPC で Redis のデータを作成 (Create)

サーバープログラム

redis_create_server.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  redis_create_server.js
//
//                  Feb/09/2020
// ---------------------------------------------------------------
var PROTO_PATH = 'redis_create.proto'

var grpc = require('grpc')

var protoLoader = require('@grpc/proto-loader')
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });
var redis_create_proto = grpc.loadPackageDefinition(packageDefinition).redis_create

// ---------------------------------------------------------------
function redis_create_proc(key_in,str_json)
{
    const redis = require ("redis")
    const client = redis.createClient (6379,'localhost')

    client.on("error", function (err)
        {
        console.log("Redis connection error to "
            + client.host + ":" + client.port + " - " + err)
        })

    client.set(key_in, str_json, redis.print)

    client.quit()
}

// ---------------------------------------------------------------
function RedisCreate(call, callback) {
    console.error("*** RedisCreate ***")
    const key_in = call.request.key
    const str_json = call.request.str_json
    const str_out = 'RedisCreate ' + key_in
    console.error(str_json)
    redis_create_proc(key_in,str_json)
    callback(null, {key: call.request.key});
}

// ---------------------------------------------------------------
function main() {
    var server = new grpc.Server()
    server.addService(redis_create_proto.Greeter.service, {RedisCreate: RedisCreate })
    server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
    server.start()
}

// ---------------------------------------------------------------
main()
// ---------------------------------------------------------------

クライアントプログラム

redis_create_client.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  redis_create_client.js
//
//                      Feb/09/2020
// ---------------------------------------------------------------
var PROTO_PATH = 'redis_create.proto'

var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    })
var redis_create_proto = grpc.loadPackageDefinition(packageDefinition).redis_create


// ---------------------------------------------------------------
function main() {
    var client = new redis_create_proto.Greeter('localhost:50051',
                                       grpc.credentials.createInsecure())

    var key_in = process.argv[2]
    var unit_aa = {}
    unit_aa.name = process.argv[3]
    unit_aa.population = process.argv[4]
    unit_aa.date_mod = process.argv[5]
    const str_json = JSON.stringify (unit_aa)
    console.error(unit_aa.name)
    client.RedisCreate({key: key_in,str_json: str_json},
        function(err, response) {
    console.log('key:', response.key)
    })

}

// ---------------------------------------------------------------
main()
// ---------------------------------------------------------------

サーバープログラムの起動

export NODE_PATH=/usr/lib/node_modules
./redis_create_server.js

クライアントプログラムの実行

$ export NODE_PATH=/usr/lib/node_modules 
$ ./redis_create_client.js t0931 那須烏山 98735 2005-6-12
那須烏山
key: t0931
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした