无法正确在Google App Engine上部署NestJS。

4

我尝试在Google App Engine上部署我的Nest.js,但无法成功部署...

复现:

nest new my-project

src/main.ts 中。
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(process.env.PORT || 8080);
}
bootstrap();

package.json 文件中:
{
    "name": "nest-gcloud-test",
    "version": "0.0.1",
    "description": "",
    "author": "",
    "private": true,
    "license": "UNLICENSED",
    "main": "dist/main.js",
    "scripts": {
        "prebuild": "rimraf dist",
        "build": "nest build",
        "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
        "start": "npm run start:dev",
        "start:dev": "node dist/main --watch",
        "start:debug": "node dist/main --debug --watch",
        "start:prod": "node dist/main",
        "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
        "test": "jest",
        "test:watch": "jest --watch",
        "test:cov": "jest --coverage",
        "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
        "test:e2e": "jest --config ./test/jest-e2e.json"
    },
    "dependencies": {
        "@nestjs/common": "^7.6.13",
        "@nestjs/core": "^7.6.13",
        "@nestjs/platform-express": "^7.6.13",
        "reflect-metadata": "^0.1.13",
        "rimraf": "^3.0.2",
        "rxjs": "^6.6.6"
    },
    "devDependencies": {
        "@nestjs/cli": "^7.5.6",
        "@nestjs/schematics": "^7.2.7",
        "@nestjs/testing": "^7.6.13",
        "@types/express": "^4.17.11",
        "@types/jest": "^26.0.20",
        "@types/node": "^14.14.31",
        "@types/supertest": "^2.0.10",
        "@typescript-eslint/eslint-plugin": "^4.15.2",
        "@typescript-eslint/parser": "^4.15.2",
        "eslint": "^7.20.0",
        "eslint-config-prettier": "^8.1.0",
        "eslint-plugin-prettier": "^3.3.1",
        "jest": "^26.6.3",
        "prettier": "^2.2.1",
        "supertest": "^6.1.3",
        "ts-jest": "^26.5.2",
        "ts-loader": "^8.0.17",
        "ts-node": "^9.1.1",
        "tsconfig-paths": "^3.9.0",
        "typescript": "^4.1.5"
    },
    "jest": {
        "moduleFileExtensions": [
            "js",
            "json",
            "ts"
        ],
        "rootDir": "src",
        "testRegex": ".*\\.spec\\.ts$",
        "transform": {
            "^.+\\.(t|j)s$": "ts-jest"
        },
        "collectCoverageFrom": [
            "**/*.(t|j)s"
        ],
        "coverageDirectory": "../coverage",
        "testEnvironment": "node"
    }
}

app.yaml

runtime: nodejs12

部署

gcloud app deploy

过了一会儿,GAE日志显示 Nest 应用程序成功启动,但GAE立即关闭并显示以下消息:

[start] 2021/03/24 20:31:36.643972 No entrypoint specified, using default entrypoint: /serve
[start] 2021/03/24 20:31:36.647170 Starting app
[start] 2021/03/24 20:31:36.647513 Executing: /bin/sh -c exec /serve
[start] 2021/03/24 20:31:36.658103 Waiting for network connection open. Subject:"app/invalid" Address:127.0.0.1:8080
[start] 2021/03/24 20:31:36.660574 Waiting for network connection open. Subject:"app/valid" Address:127.0.0.1:8081
[serve] 2021/03/24 20:31:36.682824 Serve started.
[serve] 2021/03/24 20:31:36.683836 Args: {runtimeLanguage:nodejs runtimeName:nodejs12 memoryMB:256 positional:[]}
[serve] 2021/03/24 20:31:36.686634 Running /bin/sh -c exec npm run start:dev

> nest-gcloud-test@0.0.1 start:dev /workspace
> node dist/main --watch

[32m[Nest] 31 - [39m03/24/2021, 8:31:39 PM [38;5;3m[NestFactory] [39m[32mStarting Nest application...[39m
[32m[Nest] 31 - [39m03/24/2021, 8:31:39 PM [38;5;3m[InstanceLoader] [39m[32mAppModule dependencies initialized[39m[38;5;3m +45ms[39m
[32m[Nest] 31 - [39m03/24/2021, 8:31:39 PM [38;5;3m[RoutesResolver] [39m[32mAppController {}:[39m[38;5;3m +6ms[39m
[32m[Nest] 31 - [39m03/24/2021, 8:31:39 PM [38;5;3m[RouterExplorer] [39m[32mMapped {, GET} route[39m[38;5;3m +3ms[39m
[32m[Nest] 31 - [39m03/24/2021, 8:31:39 PM [38;5;3m[NestApplication] [39m[32mNest application successfully started[39m[38;5;3m +2ms[39m
[start] 2021/03/24 20:32:53.970253 Quitting on terminated signal
[start] 2021/03/24 20:32:53.970940 Start program failed: failed to detect app after start: ForAppStart(): [aborted, context canceled. subject:"app/invalid" Timeout:30m0s, attempts:120069 aborted, context canceled. subject:"app/valid" Timeout:30m0s, attempts:120115]

stderr上没有错误日志。


然而,我可以成功地部署一个简单的HTTP服务器,如下所示:

app.js

const http = require('http');

const server = http.createServer((req, res) => {
    res.end(`Hello World\n`);
});

server.listen(process.env.PORT || 8080, () => {
    console.log('app started');
});

app.yaml

runtime: nodejs12

package.json

{
    "name": "appengine",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "start": "node app.js",
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC"
}

成功运行 gcloud app deploy 并且没有出现任何问题。


非常感谢任何帮助。

解决方案

"gcp-build": "npm run build" 添加到 package.json 中。

然后就可以正常运行而没有任何问题了。


你应该回答自己的问题!在Stack Overflow上是允许这样做的。只需将您的“编辑”移动到答案部分,然后甚至可以将您自己的答案标记为正确的解决方案。这将有助于其他人在浏览SO时快速找到答案,因为现在您的答案与您的问题合并在一起。 - fIwJlxSzApHEZIl
1个回答

3

解决方案 将"gcp-build": "npm run build"添加到package.json中

然后它就可以正常工作,没有任何问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接