堆疊溢位問題
Host Provider Heroku 似乎沒有在“npm run start”中同時接收命令。我可以讓這個啟動命令與“heroku local web”一起作業,但是在 heroku 主機上運行相同的東西時會得到以下日志錯誤。我注意到對于其他一些節點全堆疊專案,后端設定為在啟動時運行。在這些情況下,express 中有路由設定,以便請求要么路由到后端路由,要么傳遞到靜態前端檔案。我的問題是我沒有明確支持這一點。我只是在后端(或嘗試)運行 json-server,在前端運行我的 vue.js 內容。
有沒有辦法讓 Heroku 配置為同時啟動前端和后端服務器?
我可以在本地運行“npm run start”,但不能在 Heroku 上運行:似乎 Heroku(至少在默認情況下)被配置為通過運行“npm run start”來啟動節點應用程式。因此,我的 package.json 中有以下條目:
包.json
"start": "concurrently \"export NODE_ENV=production\" \"json-server --watch db.json\" \"vue-cli-service serve --mode production\""
Heroku 日志:
2021-10-17T18:30:52.000000 00:00 app[api]: Build succeeded
2021-10-17T18:30:54.001782 00:00 heroku[web.1]: Starting process with command `npm start`
2021-10-17T18:30:55.002829 00:00 app[web.1]:
2021-10-17T18:30:55.002845 00:00 app[web.1]: > my-vue-app@0.1.0 start /app
2021-10-17T18:30:55.002845 00:00 app[web.1]: > concurrently "export NODE_ENV=production" "json-server --watch db.json" "vue-cli-service serve --mode production"
2021-10-17T18:30:55.002846 00:00 app[web.1]:
2021-10-17T18:30:55.226381 00:00 app[web.1]: [1] /bin/sh: 1: json-server: not found
2021-10-17T18:30:55.227075 00:00 app[web.1]: [2] /bin/sh: 1: vue-cli-service: not found
2021-10-17T18:30:55.228104 00:00 app[web.1]: [2] vue-cli-service serve --mode production exited with code 1272021-10-17T18:30:55.228540 00:00 app[web.1]: [1] json-server --watch db.json exited with code 127
2021-10-17T18:30:55.228781 00:00 app[web.1]: [0] export NODE_ENV=production exited with code 0
2021-10-17T18:30:55.238234 00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-10-17T18:30:55.238413 00:00 app[web.1]: npm ERR! errno 1
2021-10-17T18:30:55.242355 00:00 app[web.1]: npm ERR! my-vue-app@0.1.0 start: `concurrently "export NODE_ENV=production" "json-server --watch db.json" "vue-cli-service serve --mode production"`
2021-10-17T18:30:55.242419 00:00 app[web.1]: npm ERR! Exit status 1
2021-10-17T18:30:55.242477 00:00 app[web.1]: npm ERR!
2021-10-17T18:30:55.242529 00:00 app[web.1]: npm ERR! Failed at the my-vue-app@0.1.0 start script.
2021-10-17T18:30:55.242567 00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-10-17T18:30:55.248113 00:00 app[web.1]:
2021-10-17T18:30:55.248206 00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-10-17T18:30:55.248255 00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-10-17T18_30_55_242Z-debug.log
2021-10-17T18:30:55.386385 00:00 heroku[web.1]: Process exited with status 1
2021-10-17T18:30:55.642820 00:00 heroku[web.1]: State changed from starting to crashed
2021-10-17T18:30:59.838564 00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myvueapp1.herokuapp.com request_id=eaa81f4f-8d66-4dcc-92e6-5ac730cf9c9c fwd="73.181.221.158" dyno= connect= service= status=503 bytes= protocol=https
2021-10-17T18:31:00.272840 00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=myvueapp1.herokuapp.com request_id=a3108da6-5ba3-48cf-8bc4-4b5784a6d171 fwd="73.181.221.158" dyno= connect= service= status=503 bytes= protocol=https
2021-10-17T18:32:51.000000 00:00 app[api]: Build started by user mark.johnson.
2021-10-17T18:33:54.181436 00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myvueapp1.herokuapp.com request_id=91cd8f28-f155-4b58-b4ac-bb1539e06413 fwd="73.181.221.158" dyno= connect= service= status=503 bytes= protocol=https
2021-10-17T18:33:54.362762 00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=myvueapp1.herokuapp.com request_id=8103d63b-9a7d-46f9-b2c1-5d662e77b539 fwd="73.181.221.158" dyno= connect= service= status=503 bytes= protocol=https
2021-10-17T18:34:35.091986 00:00 heroku[web.1]: State changed from crashed to starting
2021-10-17T18:34:34.685663 00:00 app[api]: Deploy 6abb4c8b by user mark.johnson.
2021-10-17T18:34:34.685663 00:00 app[api]: Release v4 created by user mark.johnson.
2021-10-17T18:34:38.788809 00:00 heroku[web.1]: Starting process with command `npm start`
2021-10-17T18:34:41.024848 00:00 app[web.1]:
2021-10-17T18:34:41.024866 00:00 app[web.1]: > my-vue-app@0.1.0 start /app
2021-10-17T18:34:41.024866 00:00 app[web.1]: > concurrently "export NODE_ENV=production" "json-server --watch db.json" "vue-cli-service serve --mode production"
2021-10-17T18:34:41.024867 00:00 app[web.1]:
2021-10-17T18:34:41.548998 00:00 app[web.1]: [1] Done
2021-10-17T18:34:41.555274 00:00 app[web.1]: [1]
2021-10-17T18:34:41.555534 00:00 app[web.1]: [1] Resources
2021-10-17T18:34:41.555535 00:00 app[web.1]: [1] http://localhost:3000/table1
2021-10-17T18:34:41.555535 00:00 app[web.1]: [1] http://localhost:3000/table2
2021-10-17T18:34:41.555535 00:00 app[web.1]: [1]
2021-10-17T18:34:41.555536 00:00 app[web.1]: [1] Home
2021-10-17T18:34:41.555536 00:00 app[web.1]: [1] http://localhost:3000
2021-10-17T18:34:41.555536 00:00 app[web.1]: [1]
2021-10-17T18:34:41.555781 00:00 app[web.1]: [1] Type s enter at any time to create a snapshot of the database
uj5u.com熱心網友回復:
正如我在這里和這里所說的,企業架構中的常見方法是通過存盤庫和服務器擁有一個應用程式。
想象一下,您的微服務 (json) 行程需要更多記憶體,因為要處理繁重的操作,并且由于您的 Web 是一個水療中心,因此不需要任何東西,因為它只是 .json 之后的靜態檔案npm run build。在這種情況下,根據基礎架構,您可以輕松地垂直/水平擴展每個工件(api 和 web)。如果您在同一臺服務器中有多個行程,那么您將稍微備份到整體架構。這只是以下幾個優點之一:
- https://michaelwashburnjr.com/blog/4-reasons-web-app-separated-frontend-backend
- https://softwareengineering.stackexchange.com/a/299550/204271
Api 和 Web
在 heroku 和任何平臺中,應用程式都有自己的埠。您還會注意到,在您的開發人員筆記本電腦中:
- vue 應用程式運行在:http://localhost:8080
- api 模擬在http://localhost:3000/posts/1
如您所見,這里有兩個應用程式,每個應用程式都有自己的埠。未來,每個人都會有自己的域名:acme.com 和 acme-api.com
僅供測驗
使用json-server將單個檔案 (db.json) 發布為微服務或 rest api 僅用于測驗或poc目的。在真實用戶的真實應用程式中并發使用會很瘋狂。
我在早期開發階段使用了它,當時 api/微服務還不存在。
當 api 準備好使用并在自己的服務器上提供自己的 http 域 ( http://acme-api.com ) 時,我在我的 web 上設定了這個新 url。
兩個應用程式(api web)
如果您不想復雜化并為真正的企業部署做好準備,您應該在您的 heroku 帳戶上創建兩個不同的應用程式
- acme-web與您的 vue 代碼。這通常需要具有
npm run dev,npm run build和npm run start。你的啟動腳本應該是這樣的:"start": "vue-cli-service serve --mode production" - acme-api與您真正的 api/微服務源代碼(資料庫連接和其他功能)。同樣在早期階段(api 尚不存在),您可以將 db.json 放在這里,您的啟動腳本應如下所示:
"start": "json-server --watch db.json"
一款應用
無論如何,如果您希望或需要在一個服務器或容器中擁有所有 (web api),您應該使用另一個提供程式,如 aws、gcp、azure 等。在該提供程式上,您的并發命令將起作用。
在heroku 上,您不能在同一個dyno 上運行兩個不同的Web 行程。
解決方法
就像 MEAN、MERN 和 MEVN 以及其他現代單體架構一樣,您可以使用快速服務器來為您的 api 和 web 提供服務。
這個應用程式應該是這樣的:
var express = require('express');
var jsonServer = require('json-server');
var app = express();
//publish the static result of npm run build (vue)
//https://github.com/jrichardsz/nodejs-express-snippets/tree/master/simple-static-server
app.use('/',
express.static(path.join(__dirname, "site" || proces.env.SITE_FOLDER)),
);
//run json-server programmatically here
//https://github.com/typicode/json-server/issues/253#issuecomment-205509836
app.use('/api', jsonServer.router('db.json'));
app.listen(3000);
環境變數
If you opt for and advanced distributed architecturem and yo have at least two environments: testing and production and you don't want to change the variables from time to time when you switch your application from your laptop to next environments dev > testing > production you will need to properly handle your environment variables using the heroku manager.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322872.html
上一篇:處理節點js中的資料庫物件
下一篇:快速驗證器不呈現錯誤前端
