我正在尝试在 heroku 平台上部署 node express 服务器,我的项目结构如下
├───build
│ ├───Models
│ │ └───DTOs
│ └───Routes
│ ├───admin
│ ├───auth
│ └───tweet
│ └───utils
├───node_modules
├───public
│ ├───images
│ └───styles
├───src
│ ├───Models
│ │ └───DTOs
│ └───Routes
│ ├───admin
│ ├───auth
│ └───tweet
│ ├───@types
│ └───utils
├───tests
│ └───Routes
│ └───auth
└───views
这是我的 package.json:
{
"repository": {
"url": "https://github.com/karafra/repost-data-bez-patosu.git",
"type": "git"
},
"name": "long-tweets",
"version": "2.0.0",
"description": "Api for matikng longer tweets",
"main": "index.js",
"directories": {
"test": "tests"
},
"dependencies": {
"cookie-parser": "^1.4.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"favicon": "0.0.2",
"firebase": "^8.2.10",
"pug": "^3.0.2",
"serve-favicon": "^2.5.0",
"ts-node": "^9.1.1",
"tslib": "^2.1.0",
"twitter-api-client": "^1.3.2",
"typescript": "^4.2.3",
"uuidv4": "^6.2.6",
"nodemon": "^2.0.7",
"supertest": "^6.1.3",
"ts-jest": "^26.5.3"
},
"devDependencies": {
"@firebase/analytics": "^0.6.4",
"@firebase/auth": "^0.16.4",
"@firebase/database": "^0.9.4",
"@firebase/firestore": "^2.1.7",
"@firebase/functions": "^0.6.2",
"@jest/types": "^26.6.2",
"@types/cookie-parser": "^1.4.2",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.20",
"@types/mocha": "^8.2.1",
"@types/node": "^14.14.32",
"@types/node-fetch": "^2.5.8",
"@types/serve-favicon": "^2.5.2",
"firestore-jest-mock": "^0.9.0",
"jest": "^26.6.3",
"jest-junit": "^12.0.0",
"ts-jest": "^26.5.3",
"supertest": "^6.1.3"
},
"scripts": {
"ts:watch": "tsc -w",
"ts:build": "tsc",
"watch": "concurrently \"npm run ts:watch\" \"npm run serve\"",
"watch:compile": "tsc -w",
"serve": "nodemon build/index.js",
"serve:prod": "node build/index.js",
"build": "npm run ts:build",
"start": "npm run build && npm run serve:prod",
"postinstall": "npm run build",
"test": "jest --unhandled-rejections=strict --colors --verbose --detectOpenHandles",
"deploy": "git add . && git commit -m 'Heroku' && git pull && git push "
},
"keywords": [
"twitter",
"twitlonger",
"bot",
"api"
],
"author": "Karafra",
"license": "ISC",
"engines": {
"node": "v14.9.0"
}
}
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"strict": true,
"baseUrl": "./",
"outDir": "build",
"sourceMap": true,
"removeComments": true,
"experimentalDecorators": true,
"target": "es6",
"strictNullChecks": false,
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"importHelpers": true,
"types": [
"node",
"jest"
],
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"./src/**/*.ts",
"./public"
],
"exclude": [
"./src/public/"
]
}
Procfile 为 web : npm start
,主 index.js 文件为
import * as path from "path";
import * as express from "express";
import { Application } from "express";
import * as coockieParser from "cookie-parser";
import { authRouter } from "./Routes/auth/AuthResource";
import { tweetRouter } from "./Routes/tweet/TweetResource";
import { adminRouter } from "./Routes/admin/AdminConsoleResource";
import { tweetLongerRouter } from "./Routes/tweet/TweetLongerResource";
// Create express app
const app: Application = express()
// Use cookie parser
app.use(coockieParser())
// Use body parser
app.use(express.json())
// Use template engine
app.set('view engine', 'pug');
app.set('views', "./views/");
app.use(express.static(path.join(__dirname, "public/")))
// Binding paths
app.use("/api", tweetLongerRouter)
app.use("/auth", authRouter)
app.use("/api", tweetRouter)
app.use("/admin", adminRouter)
//Define index route
app.get("/", (req, res) => {
if (!(req.query && Object.keys(req.query).length === 0 && req.query.constructor === Object)) {
res.redirect(307, `/auth/validate/user/?email=${req.query.username}&password=${req.query.password}`)
} else {
res.render("signUp");
}
})
//Get port from environment
const PORT: number = Number(process.env.PORT) || 5000;
//Launch app on port from config file
app.listen(PORT, () => {
console.log(`? App is running on http://localhost:${PORT}`)
})
当我在本地运行 heroku local
时一切正常,应用程序启动没有任何问题,但是当我尝试在 heroku 上部署时,构建运行正常但部署失败并显示 H10
错误代码。我尝试移动依赖项(从 devDependies 到依赖项再返回),我尝试运行在 stackoverflow 上找到的不同部署命令(因此有很多脚本),我尝试删除 dotenv.config() 调用,使用 ts-node 而不是部署tsc,删除所有环境变量并对其进行硬编码,使用构建文件夹推送代码并删除构建脚本。部署仍然失败并显示相同的 H10
错误代码。
PS.: 这是来自heroku的日志
2021-03-16T22:46:16.000000+00:00 app[api]: Build started by user
email@email.com
2021-03-16T22:47:33.558208+00:00 app[api]: Deploy 985be468 by user
email@email.com
2021-03-16T22:47:33.558208+00:00 app[api]: Release v50 created by user
email@email.com
2021-03-16T22:47:34.811352+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-16T22:47:41.135283+00:00 heroku[web.1]: Starting process with command `: npm start`
2021-03-16T22:47:42.000000+00:00 app[api]: Build succeeded
2021-03-16T22:47:43.245125+00:00 heroku[web.1]: Process exited with status 0
2021-03-16T22:47:43.317287+00:00 heroku[web.1]: State changed from starting to crashed
2021-03-16T22:48:04.609680+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myappname.heroku.com request_id=6a7c6999-f493-468d-8d5d-f5654cadcb4c fwd="178.143.32.92" dyno= connect= service= status=503 bytes= protocol=https
2021-03-16T22:48:05.091732+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=myappname.heroku.com request_id=418ab287-2b9d-4b40-a5e0-806c37bd1c11 fwd="178.143.32.92" dyno= connect= service= status=503 bytes= protocol=https