Part 3: Building gRPC Services with TypeScript and Node.js
From Zero to Production-Ready gRPC Service
Project Setup
Initial Structure
mkdir grpc-ecommerce
cd grpc-ecommerce
npm init -yDependencies
{
"name": "grpc-ecommerce",
"version": "1.0.0",
"description": "Production-ready gRPC microservices",
"main": "dist/server.js",
"scripts": {
"proto:generate": "protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./src/generated --ts_proto_opt=outputServices=grpc-js,env=node,esModuleInterop=true,useOptionals=messages ./protos/**/*.proto",
"build": "npm run proto:generate && tsc",
"dev": "npm run proto:generate && tsx watch src/server.ts",
"start": "node dist/server.js",
"lint": "eslint src --ext .ts",
"format": "prettier --write \"src/**/*.ts\""
},
"dependencies": {
"@grpc/grpc-js": "^1.9.0",
"@grpc/proto-loader": "^0.7.10",
"dotenv": "^16.3.1",
"pg": "^8.11.3",
"redis": "^4.6.10",
"winston": "^3.11.0",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/node": "^20.0.0",
"@types/pg": "^8.10.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.50.0",
"prettier": "^3.0.0",
"ts-proto": "^1.164.0",
"tsx": "^4.0.0",
"typescript": "^5.2.0"
}
}TypeScript Configuration
Project Directory Structure
Protocol Buffer Definitions
Common Types
Order Service Proto
Product Service Proto
Generate TypeScript Code
Database Configuration
PostgreSQL Setup
Redis Configuration
Logger Configuration
Repository Layer
Order Repository
Product Repository
Service Layer
Order Service
gRPC Handler
gRPC Server
Main Server Entry
Key Takeaways
PreviousPart 2: gRPC Service Design Patterns and Best PracticesNextPart 4: Authentication and Authorization in gRPC
Last updated