首页 > 解决方案 > Heroku 无法绑定 PORT

问题描述

const express = require('express')
const bodyParser = require('body-parser')
const nodemailer = require('nodemailer')
const path = require("path")
const cors = require('cors')
const ical = require('ical-generator')

require('dotenv').config()




const app = express()
const PORT = process.env.PORT || 3001;
const HOST = process.env.YOUR_HOST || '0.0.0.0'

app.use(express.urlencoded({extended:true}))
// app.use(bodyParser.json())
// app.use(bodyParser.urlencoded({extended:true}))
app.use(express.json());

app.use(cors())

if (process.env.NODE_ENV === "production") {
    app.use("/react-portofolio/", express.static(path.join(__dirname, "client", "build")));
}








app.get("*", (req, res) => {
    res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
  


// app.use(express.static(path.join(__dirname, 'build')));

// app.get('/*', (req, res) => {
//     res.sendFile(path.join(__dirname, 'build', 'index.html'));
// });
  






app.listen(PORT, HOST, () => {
  console.log(`Example app listening at http://${HOST}:${ PORT }/`)
})

抱歉打扰。已经发布了类似的问题。但是,我已经按照 3 个不同论坛上的说明进行操作,但是当我推送到 heroku 进行部署时遇到了同样的错误。构建成功,但我已经在 PORT 上运行了一些东西

2020-12-08T04:17:26.884688+00:00 app[web.1]: [1] Something is already running on port 57232.
2020-12-08T04:17:27.006588+00:00 app[web.1]: [1] npm run client exited with code 0
2020-12-08T04:26:37.127662+00:00 heroku[router]: at=info method=GET path="/" host=discoversamir.herokuapp.com request_id=46d76b48-2724-4726-9db0-6a7e32aebfc3 fwd="70.119.31.93" dyno=web.1 connect=0ms service=48ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.237894+00:00 heroku[router]: at=info method=GET path="/static/css/main.9a72c1e7.chunk.css" host=discoversamir.herokuapp.com request_id=2d0792e3-4cd4-4a05-a2a4-acc41070767d fwd="70.119.31.93" dyno=web.1 connect=0ms service=14ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.239633+00:00 heroku[router]: at=info method=GET path="/static/js/2.2cea4fb7.chunk.js" host=discoversamir.herokuapp.com request_id=dfd7e060-3da6-4073-80cd-7499cf41456a fwd="70.119.31.93" dyno=web.1 connect=0ms service=16ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.312361+00:00 heroku[router]: at=info method=GET path="/src/App.css" host=discoversamir.herokuapp.com request_id=739a09b6-4f5b-41e8-9c8a-fb05fd183303 fwd="70.119.31.93" dyno=web.1 connect=0ms service=13ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.313463+00:00 heroku[router]: at=info method=GET path="/static/js/main.dff0b724.chunk.js" host=discoversamir.herokuapp.com request_id=2e3abec9-f772-4bc8-b74b-75016552c304 fwd="70.119.31.93" dyno=web.1 connect=1ms service=14ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.399704+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=discoversamir.herokuapp.com request_id=921c7d52-76e6-4254-a9ab-cd8968dd8182 fwd="70.119.31.93" dyno=web.1 connect=0ms service=6ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.556042+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=discoversamir.herokuapp.com request_id=c58d231d-60c0-42e4-a714-30a20333a156 fwd="70.119.31.93" dyno=web.1 connect=0ms service=4ms status=200 bytes=2779 protocol=https

这是我的 pacakage.JSON

"name": "react-portofolio",
"version": "1.0.0",
"description": "My portofolio",
"main": "server.js",
"scripts": {
 
  "predeploy": "npm run build",
  "deploy": "gh-pages -d build",
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
  "start:prod": "node server.js",
  "start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
  "client": "cd client && npm run start",
  "install": "cd client && npm install",
  "build": "cd client && npm run build",
  "heroku-postbuild": "cd client && npm install && npm run build"
},
"repository": {
  "type": "git",
  "url": "git+https://github.com/agnide4/react-portofolio.git"
},
"keywords": [
  "portofolio",
  "resume"
],
"author": "Samir Bello",
"license": "ISC",
"bugs": {
  "url": "https://github.com/agnide4/react-portofolio/issues"
},
"homepage": "https://github.com/agnide4/react-portofolio#readme",
"dependencies": {
  "body-parser": "^1.19.0",
  "concurrently": "^5.3.0",
  "cors": "^2.8.5",
  "dotenv": "^8.2.0",
  "express": "^4.17.1",
  "ical-generator": "^1.15.2",
  "nodemailer": "^6.4.16",
  "nodemon": "^2.0.6"
},
"devDependencies": {
  "gh-pages": "^3.1.0"
},
"engines": {
  "node": "12.8.0",
  "npm": "6.14.9"

}

它不断给我一个 manifest.JSON 错误。回购是https://github.com/agnide4/react-portofolio。请指出我所缺少的方向。

标签: node.jsreactjsheroku

解决方案


尝试在 server.js 文件中更改它

app.listen(PORT, HOST, () => {
  console.log(`Example app listening at http://localhost:${PORT}`)
})

这个,并且还尝试删除 HOST 声明和 process.env.HOST

app.listen(PORT, () => {
  console.log(`Example app listening at http://localhost:${PORT}`)
})

推荐阅读