I'm learning nestjs and I followed this step by step.
The application works correctly. But I want to post it as a microservice in my GAE. I'm also able to do this part well, but when I call the address in the GPC, I'm getting the error 502 - Bad Gateway.
I believe it's something in my package.json file. But I have not figured it out yet. The following is the dependencies configuration:
"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"
},
This is my start instruction:
"start": "ts-node -r tsconfig-paths/register src/main.ts",
Finally, my app.yaml:
env: flex
runtime: nodejs
service: nestapp
You could start with the AppEngine Typescript sample project:
https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/typescript
It has all the typescript compilation tools setup already. The key lines you need in your package.json scripts are:
"build": "tsc -p tsconfig.build.json",
"gcp-build": "npm run build"
"gcp-build"
is a reserved task name which is always executed when deploying an AppEngine NodeJS project. This will ensure your TypeScript is compiled to JavaScript on deploy. You can read more about it here:
https://cloud.google.com/appengine/docs/standard/nodejs/running-custom-build-step
Once you have TypeScript and Express running, you can replace Express with Nest! Some other things you'll need to change, the entrypoint in package.json which AppEngine uses to run the app:
"main": "dist/main.js",
And the port in your Nest app src/main.ts:
const PORT = Number(process.env.PORT) || 8080;
await app.listen(PORT);
In your app.yaml remove env: flex
just use the standard environment, it's cheaper!
runtime: nodejs10
Putting it all together, your full package.json will look something like this:
{
"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"
}
}
I've created an example project here: