N

Node.js + Express + TypeScript Starter

Added by Gldywn
This is a template for developing a Node.js (Typescript) w/ Express app with boilerplate code targeting deployment on Phala Cloud and DStack. It includes the SDK by default to make integration with TEE features easier.
Starter

Phala Cloud Node.js + Express + TypeScript Starter

This is a template for developing a Node.js (Typescript) w/ Express app with boilerplate code targeting deployment on Phala Cloud and DStack. It includes the SDK by default to make integration with TEE features easier. This repo also includes a default Dockerfile and docker-compose.yml for deployment.

Development

Requirements

Setup

First, follow these steps to set up your development environment.

Install the Phala CLI globally:

npm i -g phala
phala help

Clone the repository:

git clone --depth 1 https://github.com/Gldywn/phala-cloud-node-starter.git
cd phala-cloud-node-starter

Install dependencies and create your .env file from the template:

npm i
cp env.example .env

DStack simulator

Before running the development server, you need to have an active DStack simulator running.

Start the simulator:

phala simulator start

# You will get something like this
✓ Setting environment for current process...
✓ DSTACK_SIMULATOR_ENDPOINT=/<user>/.phala-cloud/simulator/0.5.3/dstack.sock
✓ TAPPD_SIMULATOR_ENDPOINT=/<user>/.phala-cloud/simulator/0.5.3/tappd.sock
✓ TEE simulator started successfully

⚠️ If this is the first time you are running the simulator, replace the DSTACK_SIMULATOR_ENDPOINT variable inside your .env file with the one output by the start command.

Run

Once the simulator is running, you can start your Express development server:

npm run dev

By default, the Express development server will listen on port 3000. Open http://localhost:3000/get_quote in your browser to get a quote with Hello DStack! as report data.

This repo also includes code snippets for the following common use cases:

  • /: Returns the TCB Info of the hosted CVM.
  • /get_quote: The reportdata is Hello DStack! and generates a quote for an attestation report via the getQuote API. You can see the raw text Hello DStack! by pasting your quote into the Attestation Explorer.
  • /get_key: Basic example of the getKey API.
  • /ethereum: Using the getKey API to generate a deterministic wallet for Ethereum, a.k.a. a wallet held by the TEE instance.
  • /solana: Using the getKey API to generate a deterministic wallet for Solana, a.k.a. a wallet held by the TEE instance.

Build & Deployment

Compose: Limit Docker log size (recommended)

The provided docker-compose.yml includes a shared logging config to restrict log growth as recommended here.

Local Docker build

Build a linux/amd64 image locally via Docker:

phala docker build --image phala-cloud-node-starter --tag latest

Github Action: Docker build

The workflow .github/workflows/phala-docker-build.yml validates the Docker build. It is optional and serves as a helper if you need automated checks. On each PR and push to main, it performs a Docker build using phala docker build.

Publish to Docker public registry

Log in to Docker using Phala CLI:

phala docker login

Push your linux/amd64 image to Docker Hub:

phala docker push --image <docker-user>/phala-cloud-node-starter:latest

Deploy to Phala Cloud

Deploy a new CVM instance using your Docker Compose file:

phala deploy docker-compose.yml

# You will get something like this
CVM created successfully!

CVM ID:    xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Name:      phala-cloud-node-starter # (truncated to 20 chars)
App ID:    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Dashboard URL:  https://cloud.phala.network/dashboard/cvms/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Docs

This repository covers the basics of creating a DStack-powered application and deploying it to Phala Cloud. For more detailed information, please refer to the resources below.

Troubleshooting

This starter requires DStack v0.5 or higher for deployment on Phala Cloud, which is currently running a closed beta for this version. If your deployed CVM is not yet on v0.5, please request beta access on the official Telegram or Discord.