Google Cloud Run部署后Node.js+Prisma无法连接PostgreSQL
问题排查:Cloud Run部署Node.js+Prisma应用的数据库连接与平台参数问题
我使用Node.js搭配Prisma开发了应用后端,通过Docker构建镜像并上传至Google Cloud Artifact Registry。部署到Cloud Run后,调用注册接口(https://iXXam-connect-api-zsXXXcuetq-et.a.run.app/auth/register)时,Postman返回错误提示无法连接IP为3x.3x.2x8.1x7:5432的PostgreSQL数据库,而本地运行时可正常连接该数据库。此前尝试通过代码仓库部署时,还遇到端口监听问题,添加--platform linux/amd64参数后Google Cloud无法识别。
以下是相关配置文件详情:
Dockerfile
# Use the official Node.js image from the Docker Hub FROM node:18 # Create and change to the app directory WORKDIR /app # Copy package.json and package-lock.json to the working directory COPY package*.json ./ # Install dependencies RUN npm install # Copy the rest of the application code to the working directory COPY . ./ # Expose the port that the app runs on EXPOSE 8080 # Define the command to run the app CMD ["npm", "run", "start"]
docker-compose.yaml
version: '3.8' services: app: build: context: . dockerfile: Dockerfile container_name: ihXXmConnect-api working_dir: /app volumes: - .:/app - /app/node_modules ports: - "8080:8080" env_file: - .env depends_on: - db db: image: postgres:15 container_name: postgres-db volumes: - pgdata:/var/lib/postgresql/data env_file: - .env volumes: pgdata:
server.js
const express = require('express'); const bodyParser = require('body-parser'); const dotenv = require('dotenv'); const authController = require('./modules/authentication/auth.controller'); const voiceController = require('./modules/voice/voice.controller'); const placesController = require('./modules/places/places.controller'); // Load environment variables from .env file dotenv.config(); // Create the Express application const app = express(); const PORT = process.env.PORT || 8080; // Middleware for parsing JSON bodies app.use(bodyParser.json()); // Middleware for parsing URL-encoded bodies app.use(bodyParser.urlencoded({ extended: true })); // Serve static files from the uploads directory app.use('/uploads', express.static('uploads')); // Endpoint routes app.use('/auth', authController); app.use('/voices', voiceController); app.use('/places', placesController); // Start the server app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });
.env
DATABASE_URL="postgresql://postgres:****@3x.3x.2x8.1x7:5432/ihramdb?schema=public" JWT_SECRET=bangkit JWT_EXPIRE=30d APP_PORT=8080 APP_STAGE=development BASE_URL=http://localhost:3000/ PORT=8080 POSTGRES_PASSWORD=****** NODE_ENV=development
schema.prisma
generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } model users { id Int @id @default(autoincrement()) email String @unique password String role Int } model voices { id Int @id @default(autoincrement()) title String filePath String createdAt DateTime @default(now()) } model places { id Int @id @default(autoincrement()) place String alamat String rating Float jarakMekkah Float }
内容的提问来源于stack exchange,提问作者Daffa Saskara
相关产品推荐
相关产品推荐

