node.js - 部署 + 使用 CLI 客户端时 Heroku 应用程序错误
问题描述
我正在做一个项目,我必须让 Node.js CLI 客户端与 Heroku 服务器通信。
我尝试部署一个简单的聊天应用程序示例,以确保一切正常,但遇到 Heroku 错误,提示“应用程序发生错误,无法提供您的页面。如果您是应用程序所有者,请检查您的日志以获取详细信息. 您可以从 Heroku CLI 使用命令 heroku logs --tail 执行此操作"
这是我运行上述命令时的 Heroku 日志,我很确定该错误与服务器socket-io
在部署时无法加载模块有关:
2021-04-26T00:05:05.310536+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-04-26T00:05:05.310536+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-04-26T00_05_05_288Z-debug.log
2021-04-26T00:05:05.386444+00:00 heroku[web.1]: Process exited with status 1
2021-04-26T00:05:05.490453+00:00 heroku[web.1]: State changed from starting to crashed
2021-04-26T00:05:05.494874+00:00 heroku[web.1]: State changed from crashed to starting
2021-04-26T00:05:08.142848+00:00 heroku[web.1]: Starting process with command `npm start`
2021-04-26T00:05:11.291774+00:00 app[web.1]:
2021-04-26T00:05:11.291802+00:00 app[web.1]: > test@1.0.0 start /app
2021-04-26T00:05:11.291802+00:00 app[web.1]: > node index.js
2021-04-26T00:05:11.291802+00:00 app[web.1]:
2021-04-26T00:05:11.374875+00:00 app[web.1]: internal/modules/cjs/loader.js:883
2021-04-26T00:05:11.374878+00:00 app[web.1]: throw err;
2021-04-26T00:05:11.374879+00:00 app[web.1]: ^
2021-04-26T00:05:11.374879+00:00 app[web.1]:
2021-04-26T00:05:11.374880+00:00 app[web.1]: Error: Cannot find module 'socket.io'
2021-04-26T00:05:11.374880+00:00 app[web.1]: Require stack:
2021-04-26T00:05:11.374880+00:00 app[web.1]: - /app/index.js
2021-04-26T00:05:11.374881+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
2021-04-26T00:05:11.374881+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:725:27)
2021-04-26T00:05:11.374882+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:952:19)
2021-04-26T00:05:11.374882+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:88:18)
2021-04-26T00:05:11.374882+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:2:12)
2021-04-26T00:05:11.374883+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)
2021-04-26T00:05:11.374883+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-04-26T00:05:11.374883+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-04-26T00:05:11.374887+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-04-26T00:05:11.374888+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
2021-04-26T00:05:11.374888+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2021-04-26T00:05:11.374889+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2021-04-26T00:05:11.374889+00:00 app[web.1]: }
2021-04-26T00:05:11.407116+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-04-26T00:05:11.407516+00:00 app[web.1]: npm ERR! errno 1
2021-04-26T00:05:11.416214+00:00 app[web.1]: npm ERR! test@1.0.0 start: `node index.js`
2021-04-26T00:05:11.416464+00:00 app[web.1]: npm ERR! Exit status 1
2021-04-26T00:05:11.416760+00:00 app[web.1]: npm ERR!
2021-04-26T00:05:11.416976+00:00 app[web.1]: npm ERR! Failed at the test@1.0.0 start script.
2021-04-26T00:05:11.417532+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-04-26T00:05:11.430755+00:00 app[web.1]:
2021-04-26T00:05:11.431385+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-04-26T00:05:11.431600+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-04-26T00_05_11_418Z-debug.log
2021-04-26T00:05:11.505552+00:00 heroku[web.1]: Process exited with status 1
2021-04-26T00:05:11.580749+00:00 heroku[web.1]: State changed from starting to crashed
2021-04-26T00:05:13.627279+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=slingshot-test-nodejs.herokuapp.com request_id=41d7f800-0833-45ac-9a24-e9e984981929 fwd="24.196.239.75" dyno= connect= service= status=503 bytes= protocol=https
2021-04-26T00:05:13.811879+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=slingshot-test-nodejs.herokuapp.com request_id=de243ea9-c7e1-4a74-91bd-4da78834b085 fwd="24.196.239.75" dyno= connect= service= status=503 bytes= protocol=https
2021-04-26T00:13:06.000000+00:00 app[api]: Build started by user hbagalkote@gmail.com
2021-04-26T00:13:23.000000+00:00 app[api]: Build succeeded
2021-04-26T00:13:23.403605+00:00 app[api]: Deploy 12d7e839 by user hbagalkote@gmail.com
2021-04-26T00:13:23.403605+00:00 app[api]: Release v8 created by user hbagalkote@gmail.com
2021-04-26T00:13:23.552491+00:00 heroku[web.1]: State changed from crashed to starting
2021-04-26T00:13:26.818012+00:00 heroku[web.1]: Starting process with command `node index.js`
2021-04-26T00:13:29.914344+00:00 app[web.1]: internal/modules/cjs/loader.js:883
2021-04-26T00:13:29.914388+00:00 app[web.1]: throw err;
2021-04-26T00:13:29.914388+00:00 app[web.1]: ^
2021-04-26T00:13:29.914389+00:00 app[web.1]:
2021-04-26T00:13:29.914389+00:00 app[web.1]: Error: Cannot find module 'socket.io'
2021-04-26T00:13:29.914390+00:00 app[web.1]: Require stack:
2021-04-26T00:13:29.914390+00:00 app[web.1]: - /app/index.js
2021-04-26T00:13:29.914391+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
2021-04-26T00:13:29.914391+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:725:27)
2021-04-26T00:13:29.914391+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:952:19)
2021-04-26T00:13:29.914392+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:88:18)
2021-04-26T00:13:29.914392+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:2:12)
2021-04-26T00:13:29.914392+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)
2021-04-26T00:13:29.914392+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-04-26T00:13:29.914393+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-04-26T00:13:29.914393+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-04-26T00:13:29.914394+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
2021-04-26T00:13:29.914394+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2021-04-26T00:13:29.914394+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2021-04-26T00:13:29.914395+00:00 app[web.1]: }
2021-04-26T00:13:29.974925+00:00 heroku[web.1]: Process exited with status 1
2021-04-26T00:13:30.067473+00:00 heroku[web.1]: State changed from starting to crashed
2021-04-26T00:13:32.450999+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=slingshot-test-nodejs.herokuapp.com request_id=37d17d3f-c2b0-4bca-b1ca-36eb952e09de fwd="24.196.239.75" dyno= connect= service= status=503 bytes= protocol=https
2021-04-26T00:13:32.618072+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=slingshot-test-nodejs.herokuapp.com request_id=13ff0bcf-2201-46d9-84ac-d05c91bd12ef fwd="24.196.239.75" dyno= connect= service= status=503 bytes= protocol=https
2021-04-26T00:16:34.224143+00:00 heroku[web.1]: State changed from crashed to starting
2021-04-26T00:16:36.722190+00:00 heroku[web.1]: Starting process with command `node index.js`
2021-04-26T00:16:38.725268+00:00 app[web.1]: internal/modules/cjs/loader.js:883
2021-04-26T00:16:38.725295+00:00 app[web.1]: throw err;
2021-04-26T00:16:38.725296+00:00 app[web.1]: ^
2021-04-26T00:16:38.725296+00:00 app[web.1]:
2021-04-26T00:16:38.725296+00:00 app[web.1]: Error: Cannot find module 'socket.io'
2021-04-26T00:16:38.725296+00:00 app[web.1]: Require stack:
2021-04-26T00:16:38.725297+00:00 app[web.1]: - /app/index.js
2021-04-26T00:16:38.725297+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
2021-04-26T00:16:38.725297+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:725:27)
2021-04-26T00:16:38.725298+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:952:19)
2021-04-26T00:16:38.725298+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:88:18)
2021-04-26T00:16:38.725298+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:2:12)
2021-04-26T00:16:38.725298+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)
2021-04-26T00:16:38.725299+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-04-26T00:16:38.725299+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-04-26T00:16:38.725299+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-04-26T00:16:38.725300+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
2021-04-26T00:16:38.725300+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2021-04-26T00:16:38.725300+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2021-04-26T00:16:38.725301+00:00 app[web.1]: }
2021-04-26T00:16:38.775108+00:00 heroku[web.1]: Process exited with status 1
2021-04-26T00:16:38.832146+00:00 heroku[web.1]: State changed from starting to crashed
2021-04-26T00:17:40.388694+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=slingshot-test-nodejs.herokuapp.com request_id=f42d0aaf-a1db-4f6a-8888-063084139ca2 fwd="24.196.239.75" dyno= connect= service= status=503 bytes= protocol=https
2021-04-26T00:17:40.499806+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=slingshot-test-nodejs.herokuapp.com request_id=34f18224-8dfd-40aa-93db-78ad61880e63 fwd="24.196.239.75" dyno= connect= service= status=503 bytes= protocol=https
此外,我web: node index.js
在我的 Procfile 中有。
我不太清楚为什么会发生这种情况,因为我已经socket-io
在本地安装并且在本地测试时服务器和客户端工作正常。
我的第二个问题是关于客户端和服务器代码。由于我可以在本地测试时硬编码端口值,因此客户端和服务器可以通信。但是,我如何让客户端与 Heroku 中运行的代码交谈index.js
(假设我解决了第一个问题)?
我的客户端(client.js)和服务器(index.js)代码如下:
//server code
const http = require('http').createServer()
const io = require('socket.io')(http)
const port = process.env.PORT || 3000
http.listen(port, () => console.log('server listening on port: ' + port))
io.on('connection', (socket) => {
console.log('connected')
socket.on('message', (evt) => {
console.log(evt)
socket.broadcast.emit('message', evt)
})
})
//client code
//Obviously, this localhost value will not work, but what should I change it to instead?
var socket = require('socket.io-client')('http://localhost:3000')
const repl = require('repl')
const chalk = require('chalk')
var username = null
socket.on('disconnect', function() {
socket.emit('disconnect')
})
socket.on('connect', () => {
console.log(chalk.red('== start chatting =='))
username = process.argv[2]
})
socket.on('message', (data) => {
const { cmd, username } = data
console.log(chalk.green(username + ': ' + cmd.split("\n")[0]))
})
repl.start({
prompt: '',
eval: (cmd) => {
socket.send({cmd, username})
}
})
解决方案
推荐阅读
- sql - Oracle 数据库 11gR2 快捷版
- spring-boot - 没有可用于服务的服务器:localhost
- c# - Assembly-CSharp.csproj 错误和 Intellisense 在 VSCode 中不起作用
- spring-boot - 使用 Spring Webflux 实现 trace-id
- c# - 获取调查 C# 中选择的用户数
- c# - ListView 在更新其屏幕后不保存更改
- postgresql - 在使用 \COPY 导入数据时,在 Postgres 中生成自定义列值的最快方法是什么?
- reactjs - React - 复合组件(对象作为 React 子级无效)
- python - 通过 django 中的 url 调用 API
- python - 如何找到具有基本事实(完美图像)的数据集来比较孔填充方法?