Create and configure the project

You will create the Node.js project using npm. Akka Serverless services rely on gRPC and Protobuf to communicate with other services. You will add gRPC, Protobuf, and Akkaserverless dependencies to the package.json file and scripts to compile the Protobuf files and start the gRPC server. Finally, you will create a .js file as an entry point to the application.

I. Create new Node.js project

Follow these steps to create your project:

  1. Create a directory on your local system for the function code:

    mkdir ~/shoppingcart
    cd shoppingcart
  2. Run the npm init command to create a package.json file. Accept the defaults except for the test command where you need to specify mocha, which you will use to run unit tests.

    npm init

    The output should look similar to the following:

    $ npm init
    ... ## edited for brevity
    test command: mocha
    ... ## edited for brevity
    {
      "name": "shoppingcart",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "mocha"
      },
      "author": "",
      "license": "ISC"
    }
    
    Is this OK? (yes) yes

II. Add dependencies

Services rely on gRPC and Protobuf to communicate with other services. The dependencies @grpc/proto-loader, google-protobuf, and grpc make sure your project works with those formats. The akkaserverless-javascript-sdk dependency is a language library that makes building projects more straightforward. Add the dependencies as follows:

  1. Add the language SDK either manually, or by running the commands:

    npm install @lightbend/akkaserverless-javascript-sdk@0.7.0-beta.1
  2. Add specific dependencies to handle gRPC communication either manually, or by running the commands:

    npm install @grpc/proto-loader
    npm install google-protobuf
    npm install grpc
  3. Add the dependencies to test your function code either manually, or by running the commands:

    npm install chai --save-dev
    npm install mocha --save-dev
  4. Add additional lines to the scripts section of the package.json file to compile the protobuf files and start the gRPC server. The scripts section should look like the following:

    {
      "scripts": {
        "test": "mocha ./test/*.test.js",
        "pretest": "compile-descriptor ./cart.proto ./domain.proto",
        "build": "compile-descriptor ./cart.proto ./domain.proto",
        "start": "node index.js"
      },
    }

The completed package.json file

Your complete package.json file should look like the following:

{
  "name": "shoppingcart",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha ./test/*.test.js",
    "pretest": "compile-descriptor ./cart.proto ./domain.proto",
    "build": "compile-descriptor ./cart.proto ./domain.proto",
    "start": "node index.js"
  },
  "type": "module",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@grpc/proto-loader": "^0.5.5",
    "akkaserverless": "0.7.0-beta.1",
    "google-protobuf": "^3.14.0",
    "grpc": "^1.24.4"
  },
  "devDependencies": {
    "chai": "^4.3.4",
    "mocha": "^8.2.1"
  }
}