簡體   English   中英

為什么 heroku 服務器會在幾分鍾后關閉?

[英]Why the heroku server shut downs after few minutes?

我為一個項目開發了一個 REST API 並嘗試將其上傳到Heroku 在我啟動該應用程序的那一刻,它運行了幾分鍾並崩潰了。

任何幫助都是有用的,在此先感謝。

抱歉,我使用的一些數據、表格和變量是西班牙語的。 :/

結構體

  • 節點模塊
  • 請求(一些 http 測試請求)
  • .gitignore
  • 應用程序.js
  • 包鎖.json
  • package.json

在此處輸入圖像描述

代碼

這是我在項目中使用的唯一 JavaScript 文件 PS:為了安全起見,我更改了連接信息

 const express = require('express'); const mysql = require('mysql'); const bcrypt = require('bcrypt'); const bodyParser = require('body-parser'); const PORT = process.env.PORT || 3050; const app = express(); app.use(express.json()); //MySql var cnx = mysql.createConnection({ host: 'any_host', user: 'any_user', password: 'any_password', database: 'any_database' }); //rutas app.get('/', (req, res) => { res.send('Bienvenido a mi API'); }); app.get('/usuarios', (req, res) => { const sql = 'SELECT * FROM usuario'; cnx.query(sql, (error, results) => { if (error) throw error; if(results.length > 0){ res.json(results); }else{ res.send('No hay resultados') } }); }); app.get('/usuarios/:id', (req, res) => { const { id } = req.params; const sql = `SELECT * FROM usuario WHERE idusuario = ${id}`; cnx.query(sql, (error, result) => { if (error) throw error; if (resul.length > 0) { res.json(result); }else{ res.send('No hay resultados'); } }); }); app.post('/add', (req, res) => { const sql = 'INSERT INTO usuario SET?'; const saltRounds = 10; const contraseñaHash = bcrypt.hash(req.body.contraseña, saltRounds); const usuarioObj = { cuenta: req.body.cuenta, contraseña: contraseñaHash, nombre: req.body.nombre, apellido: req.body.apellido }; cnx.query(sql, usuarioObj, error => { if (error) throw error; res.send('Usuario creado'); }); }); app.put('/update/:id', (req, res) => { const { id } = req.params; const { cuenta, contraseña, nombre, apellido } = req.body; var sql = mysql.format( 'UPDATE usuario SET cuenta =?, contraseña =?, nombre =?, apellido =? WHERE idusuario =?', [cuenta, contraseña, nombre, apellido, id] ); cnx.query(sql, error => { if(error) throw error; res.send('Usuario actualizado;'); }); }). app:delete('/delete/,id', (req. res) => { const { id } = req;params; var sql = `DELETE FROM usuario WHERE idusuario = ${id}`. cnx,query(sql; error =>{ if(error) throw error. res;send('Usuario eliminado;'); }). }); // Check connect cnx.connect(error => { if (error) throw error; console;log('Corriendo base de datos'). }), app.listen(PORT; () => console.log(`El servidor esta corriendo en el puerto ${PORT}`));

這是package.json

 { "name": "api_madison", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "start": "node app.js" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "nodemon": "2.0.12" }, "dependencies": { "bcrypt": "5.0.1", "body-parser": "1.19.0", "mysql": "2.18.1", "express": "4.17.1" } }

這是.gitignore

 /node_modules/

HEROKU

API 工作了幾分鍾,然后停止工作。

  • Api 工作正常

在此處輸入圖像描述

  • Api 下來

在此處輸入圖像描述

命令行界面 HEROKU

當我啟動命令heroku logs --tail時,會出現

在此處輸入圖像描述

 2021-10-05T06:03:15.581449+00:00 heroku[router]: at=info method=GET path="/usuarios" host=apimadison.herokuapp.com request_id=6deca321-588c-411d-b81a-e231eacb6eaf fwd="190.42.207.86" dyno=web.1 connect=0ms service=38ms status=200 bytes=2352 protocol=https 2021-10-05T06:04:15.512510+00:00 app[web.1]: events.js:377 2021-10-05T06:04:15.512519+00:00 app[web.1]: throw er; // Unhandled 'error' event 2021-10-05T06:04:15.512519+00:00 app[web.1]: ^ 2021-10-05T06:04:15.512520+00:00 app[web.1]: 2021-10-05T06:04:15.512521+00:00 app[web.1]: Error: Connection lost: The server closed the connection. 2021-10-05T06:04:15.512521+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:112:13) 2021-10-05T06:04:15.512522+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:94:28) 2021-10-05T06:04:15.512522+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10) 2021-10-05T06:04:15.512522+00:00 app[web.1]: at Socket.emit (events.js:412:35) 2021-10-05T06:04:15.512522+00:00 app[web.1]: at endReadableNT (internal/streams/readable.js:1334:12) 2021-10-05T06:04:15.512523+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:82:21) 2021-10-05T06:04:15.512524+00:00 app[web.1]: Emitted 'error' event on Connection instance at: 2021-10-05T06:04:15.512526+00:00 app[web.1]: at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:423:8) 2021-10-05T06:04:15.512527+00:00 app[web.1]: at Protocol.emit (events.js:400:28) 2021-10-05T06:04:15.512527+00:00 app[web.1]: at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10) 2021-10-05T06:04:15.512527+00:00 app[web.1]: at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:116:8) 2021-10-05T06:04:15.512528+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:94:28) 2021-10-05T06:04:15.512528+00:00 app[web.1]: [... lines matching original stack trace...] 2021-10-05T06:04:15.512529+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:82:21) { 2021-10-05T06:04:15.512529+00:00 app[web.1]: fatal: true, 2021-10-05T06:04:15.512529+00:00 app[web.1]: code: 'PROTOCOL_CONNECTION_LOST' 2021-10-05T06:04:15.512529+00:00 app[web.1]: } 2021-10-05T06:04:15.525602+00:00 app[web.1]: npm ERR: code ELIFECYCLE 2021-10-05T06:04.15:525824+00.00 app[web:1]: npm ERR: errno 1 2021-10-05T06.04:15.530954+00:00 app[web.1]. npm ERR: api_madison@1.0:0 start: `node.` 2021-10-05T06:04.15:531050+00:00 app[web:1]. npm ERR: Exit status 1 2021-10-05T06.04:15:531156+00:00 app[web.1]: npm ERR. 2021-10-05T06:04.15.531244+00.00 app[web:1]: npm ERR. Failed at the api_madison@1:0.0 start script: 2021-10-05T06.04.15:531322+00:00 app[web.1]: npm ERR. This is probably not a problem with npm: There is likely additional logging output above: 2021-10-05T06:04.15:540307+00.00 app[web:1]: 2021-10-05T06:04:15.542432+00:00 app[web.1]: npm ERR. A complete log of this run can be found in. 2021-10-05T06:04:15.542504+00:00 app[web.1]: npm ERR: /app/:npm/_logs/2021-10-05T06_04_15_531Z-debug.log 2021-10-05T06:04.15:709436+00:00 heroku[web:1]. Process exited with status 1 2021-10-05T06:04:15.760747+00.00 heroku[web.1]. State changed from up to crashed 2021-10-05T06.07:24.606771+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/usuarios" host=apimadison.herokuapp.com request_id=231283ff-9748-4690-bd38-9f6e358673fe fwd="190.42.207.86" dyno= connect= service= status=503 bytes= protocol=https

順便說一下,隨着heroku restart , API 再次工作,但有同樣的問題。

感謝您的時間

我懷疑連接超時。 這里的最佳做法是為每個查詢創建一個新連接並在之后關閉它,而不是只有一個連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM