繁体   English   中英

如何在 Google App Engine 中发布 Nestjs api?

[英]How to publish Nestjs api in Google App Engine?

我正在尝试将我的 nestjs api 放入 Google App Engine,但我仍然有错误。 我首先使用谷歌 sdk 创建了我的谷歌云项目,编辑我的代码如下:

main.ts:

import {NestFactory} from '@nestjs/core';
import {DocumentBuilder, SwaggerModule} from '@nestjs/swagger';
import {AppModule} from './app.module';
import {ValidationPipe} from "@nestjs/common";
import * as cookieParser from 'cookie-parser';
import * as helmet from 'helmet';
import * as Express from 'express';
import { ExpressAdapter } from '@nestjs/platform-express';


const server = Express();
server.get('/',(req,res) => res.send('ok'));
server.get('/_ah/health',(req,res) => res.send('ok'));

async function bootstrap() {
    const app = await NestFactory.create(AppModule, new ExpressAdapter(server), { cors: true });

    //Validation
    app.useGlobalPipes(new ValidationPipe({
        whitelist: true,
    }));

    //Swagger
    const config = new DocumentBuilder()
        .setTitle('Title')
        .setDescription('Description')
        .addBearerAuth()
        .setVersion('2.0')
        .build();
    const document = SwaggerModule.createDocument(app, config);
    SwaggerModule.setup('doc', app, document);

    //Use Cookies
    app.use(cookieParser());

    //Security
    //1 - Helmet
    app.use(helmet());
    //2 - CORS :/
    app.enableCors();


    await app.listen(8080);
}

bootstrap();

package.json:

{
  "name": "name",
  "version": "2.0.0",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "main": "dist/main.js",
  "engines": {
    "node": ">=8.0.0"
  },
  "scripts": {
    "build": "nest build",
    "prepare": "npm run gcp-build",
    "pretest": "npm run gcp-build",
    "posttest": "npm run lint",
    "prebuild": "rimraf dist",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "node ./index.js",
    "start:dev": "nodemon",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "tslint -p .",
    "test": "mocha test/*.test.js --exit",
    "compodoc": "npx compodoc -p tsconfig.app.json src -n \"Elengui REST API 2.0\" -r 3001 --theme readthedocs --hideGenerator",
    "gcp-build": "npm run build",
    "deploy": "gcloud app deploy"
  },
  "dependencies": {
    "@hapi/joi": "^17.1.1",
    "@nestjs/common": "^7.5.1",
    "@nestjs/config": "^0.6.3",
    "@nestjs/core": "^7.5.1",
    "@nestjs/jwt": "^7.2.0",
    "@nestjs/mapped-types": "^0.3.0",
    "@nestjs/passport": "^7.1.5",
    "@nestjs/platform-express": "^7.5.1",
    "@nestjs/swagger": "^4.7.13",
    "@nestjs/throttler": "^1.1.3",
    "@nestjs/typeorm": "^7.1.5",
    "@types/bcrypt": "^3.0.0",
    "@types/cookie-parser": "^1.4.2",
    "@types/hapi__joi": "^17.1.6",
    "@types/nodemailer": "^6.4.0",
    "@types/passport": "^1.0.6",
    "@types/passport-jwt": "^3.0.4",
    "@types/passport-local": "^1.0.33",
    "bcrypt": "^5.0.0",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "cookie-parser": "^1.4.5",
    "crypto-random-string": "^3.3.1",
    "csurf": "^1.11.0",
    "hbs": "^4.1.1",
    "helmet": "^4.4.1",
    "mysql2": "^2.2.5",
    "nodemailer": "^6.5.0",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "path": "^0.12.7",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.3",
    "swagger-ui-express": "^4.1.6",
    "typeorm": "^0.2.31",
    "express": "^4.16.3"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.5.4",
    "@nestjs/schematics": "^7.1.3",
    "@nestjs/testing": "^7.5.1",
    "@types/express": "^4.16.0",
    "@types/jest": "^26.0.15",
    "@types/node": "^14.14.6",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.6.1",
    "@typescript-eslint/parser": "^4.6.1",
    "eslint": "^7.12.1",
    "eslint-config-prettier": "7.2.0",
    "eslint-plugin-prettier": "^3.1.4",
    "jest": "^26.6.3",
    "prettier": "^2.1.2",
    "supertest": "^6.0.0",
    "ts-jest": "^26.4.3",
    "ts-loader": "^8.0.8",
    "ts-node": "^9.0.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.0.5",
    "mocha": "^8.0.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

当我尝试部署项目时,我收到此错误:

> nest build

sh: 1: nest: not found
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! elengui-api@2.0.0 build: `rm -rf dist && tsc -p tsconfig.build.json`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the elengui-api@2.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我已经完成了“npm install”,但脚本似乎无法识别命令嵌套。 关于发生了什么的任何想法? 提前致谢

看看另一个帖子:

找不到嵌套命令

看来您需要安装和使用 npm 为:

@nestjs/cli而不仅仅是nest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM