我正在学习nestjs,我遵循了step by step。
该应用程序正常运行。但是我想将其作为微服务发布到我的GAE中。我也能做得很好,但是当我在GPC中调用地址时,出现错误502-错误的网关。
我相信这是我的package.json文件中的内容。但是我还没有弄清楚。以下是依赖项配置:
"dependencies": {
"@ nestjs / common": "^ 5.0.0",
"@ nestjs / core": "^ 5.0.0",
"@ nestjs / microservices": "^ 5.0.0",
"@ nestjs / testing": "^ 5.0.0",
"@ nestjs / websockets": "^ 5.0.0",
"reflect-metadata": "^ 0.1.12",
"rxjs": "^ 6.0.0",
"typescript": "^ 2.8.0",
"ts-node": "^ 6.0.0",
"tsconfig-paths": "^ 3.3.1"
},
这是我的入门指导:
"start": "ts-node -r tsconfig-paths/register src/main.ts",
最后,我的app.yaml:
env: flex
runtime: nodejs
service: nestapp
答案 0 :(得分:1)
您可以从AppEngine Typescript示例项目开始:
https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/typescript
它已经具有所有打字稿编译工具的设置。您需要的关键行在package.json脚本中:
"build": "tsc -p tsconfig.build.json",
"gcp-build": "npm run build"
"gcp-build"
是保留的任务名称,在部署AppEngine NodeJS项目时始终如此。这将确保您的TypeScript在部署时被编译为JavaScript。您可以在此处了解更多信息:
https://cloud.google.com/appengine/docs/standard/nodejs/running-custom-build-step
一旦您运行了TypeScript和Express,就可以将Nest替换成Express!您还需要更改其他一些东西,即AppEngine用于运行应用程序的package.json中的入口点:
"main": "dist/main.js",
Nest应用程序src / main.ts中的端口:
const PORT = Number(process.env.PORT) || 8080;
await app.listen(PORT);
在您的app.yaml中,仅使用标准环境删除env: flex
,它更便宜!
runtime: nodejs10
最后,完整的package.json文件如下所示:
{
"name": "appengine-nest",
"description": "An example TypeScript app running on Google App Engine.",
"version": "0.0.1",
"author": "kmturley",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
},
"main": "dist/main.js",
"scripts": {
"prepare": "npm run build",
"pretest": "npm run build",
"build": "tsc -p tsconfig.build.json",
"deploy": "gcloud app deploy",
"lint": "tslint -p tsconfig.json -c tslint.json",
"start": "node ./dist/main.js",
"start:dev": "nodemon",
"start:debug": "nodemon --config nodemon-debug.json",
"test": "repo-tools test app -- dist/main.js",
"gcp-build": "npm run build"
},
"dependencies": {
"@nestjs/common": "^5.6.2",
"@nestjs/core": "^5.6.2",
"express": "^4.16.3",
"nodemon": "^1.18.9",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.3.3",
"ts-node": "^8.0.2",
"tsconfig-paths": "^3.7.0",
"typescript": "^3.0.1"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"@types/express": "^4.16.0",
"tslint": "^5.11.0"
}
}
我在这里创建了一个示例项目:
答案 1 :(得分:1)
所需的更改
nest new <project>
是:
package.json (主要属性和一些脚本)
{
...
+ "main": "dist/main.js",
"scripts": {
...
- "build": "nest build",
+ "build": "tsc -p tsconfig.build.json",
+ "gcp-build": "npm run build",
...
- "start": "nest start",
+ "start": "node ./dist/main.js",
}
...
}
src/main.ts (添加要监听的端口环境变量)
async function bootstrap() {
const app = await NestFactory.create(AppModule);
- await app.listen(3000);
+ const PORT = Number(process.env.PORT) || 8080;
+ await app.listen(PORT);
}
bootstrap();
另外建议在 package.json 脚本上添加部署脚本
使用 devDependency @google-cloud/nodejs-repo-tools
+ "deploy": "gcloud app deploy"
注意:解决方案是@Kim T,我只是以这种方式添加它,因为我无法快速阅读和执行它
答案 2 :(得分:0)
我认为您需要pm2之类的东西,或者需要pm2的docker。因为您需要启动服务器并侦听特定端口上的妖魔。希望对您有帮助