Nest.JS部署到Heroku [英] Nest.JS deploy to Heroku

查看:133
本文介绍了Nest.JS部署到Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试部署Nest.JS CLI(从5.3.0版开始)生成的基本启动模板,并且似乎在应用启动时超时.我想知道是否有人设法部署到Heroku?

I'm currently trying to deploy the basic starter template generated by the Nest.JS CLI (as of version 5.3.0) and seem to be getting a timeout on app startup. I'm wondering if anyone has managed to deploy to Heroku?

我不确定是否需要包含某种Procfile?

I'm not sure whether or not I need to include some kind of Procfile?

此外,关于部署Nest.JS似乎没有太多信息

Also, there doesn't seem to be much info around deploying the Nest.JS

当我尝试部署时,Heroku会记录日志.

The Heroku logs when I try to deploy.

2018-10-16T06:52:09.602465+00:00 heroku[web.1]: Starting process with 
command `npm start`
2018-10-16T06:52:12.281532+00:00 app[web.1]: 
2018-10-16T06:52:12.281553+00:00 app[web.1]: > testy@0.0.0 start /app
2018-10-16T06:52:12.281555+00:00 app[web.1]: > ts-node -r tsconfig-        
paths/register src/main.ts
2018-10-16T06:52:12.281556+00:00 app[web.1]: 
2018-10-16T06:52:17.557991+00:00 app[web.1]: [Nest] 21   - 2018-10-16 
06:52:17   [NestFactory] Starting Nest application...
2018-10-16T06:52:17.578565+00:00 app[web.1]: [Nest] 21   - 2018-10-16 
06:52:17   [InstanceLoader] AppModule dependencies initialized +21ms
2018-10-16T06:52:17.626325+00:00 app[web.1]: [Nest] 21   - 2018-10-16 
06:52:17   [RoutesResolver] AppController {/}: +48ms
2018-10-16T06:52:17.633303+00:00 app[web.1]: [Nest] 21   - 2018-10-16 
06:52:17   [RouterExplorer] Mapped {/, GET} route +7ms
2018-10-16T06:52:17.636215+00:00 app[web.1]: [Nest] 21   - 2018-10-16 
06:52:17   [NestApplication] Nest application successfully started +3ms
2018-10-16T06:53:09.948188+00:00 app[web.1]: Error waiting for process 
to terminate: No child processes
2018-10-16T06:53:09.928492+00:00 heroku[web.1]: Error R10 (Boot 
timeout) -> Web process failed to bind to $PORT within 60 seconds of 
launch
2018-10-16T06:53:09.928658+00:00 heroku[web.1]: Stopping process with 
SIGKILL
2018-10-16T06:53:10.046177+00:00 heroku[web.1]: Process exited with 
status 22
2018-10-16T06:53:10.063329+00:00 heroku[web.1]: State changed from 
starting to crashed
2018-10-16T06:53:10.064950+00:00 heroku[web.1]: State changed from 
crashed to starting

我的package.json文件在下面...

my package.json file is below...

{
  "name": "testy",
  "version": "0.0.0",
  "description": "description",
  "author": "",
  "license": "MIT",
  "scripts": {
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "nodemon",
    "start:debug": "nodemon --config nodemon-debug.json",
    "prestart:prod": "rimraf dist && tsc",
    "start:prod": "node dist/main.js",
    "start:hmr": "node dist/server",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "webpack": "webpack --config webpack.config.js"
  },
  "dependencies": {
    "@nestjs/common": "^5.1.0",
    "@nestjs/core": "^5.1.0",
    "reflect-metadata": "^0.1.12",
    "rxjs": "^6.2.2",
    "typescript": "^3.0.1"
  },
  "devDependencies": {
    "@nestjs/testing": "^5.1.0",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.1",
    "@types/node": "^10.7.1",
    "@types/supertest": "^2.0.5",
    "jest": "^23.5.0",
    "nodemon": "^1.18.3",
    "prettier": "^1.14.2",
    "rimraf": "^2.6.2",
    "supertest": "^3.1.0",
    "ts-jest": "^23.1.3",
    "ts-loader": "^4.4.2",
    "ts-node": "^7.0.1",
    "tsconfig-paths": "^3.5.0",
    "tslint": "5.11.0",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-node-externals": "^1.7.2"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

如果有人在部署此堆栈方面有任何经验,很高兴收到您的来信

If anyone has any experience in deploying this stack it'd be great to hear from you

推荐答案

Heroku默认情况下为您分配一个端口,并将该端口添加到环境变量(env)中,因此您可以将端口设置为固定数字,您需要将您的主文件更改为:

Heroku assigns you a port by default and adds the port to the environment variables (env), so you can set the port to a fixed number, you need to change your main file to:

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 || 3000);
}
bootstrap();

这篇关于Nest.JS部署到Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆