错误R10(启动超时)->;Web进程无法在启动后60秒内绑定到$Port-Heroku错误
我将Node.js WebApp部署到Heroku,但收到此错误
2021-06-01T09:19:42.615419+00:00 heroku[web.1]: State changed from crashed to starting
2021-06-01T09:19:47.259832+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-06-01T09:19:51.146182+00:00 app[web.1]: Server is running on port 3001.
2021-06-01T09:20:47.916699+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to
bind to $PORT within 60 seconds of launch
2021-06-01T09:20:47.989032+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-06-01T09:20:48.124402+00:00 heroku[web.1]: Process exited with status 137
2021-06-01T09:20:48.196055+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-01T09:24:45.072782+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET
path="/" host=positate.herokuapp.com request_id=7e9ec2b1-5685-4c3f-9c29-6c03268b7c82
fwd="157.51.56.186" dyno= connect= service= status=503 bytes= protocol=https
2021-06-01T09:24:46.540443+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET
path="/favicon.ico" host=positate.herokuapp.com request_id=4ab44a6a-4795-4a4d-b32d-28d796845774
fwd="157.51.56.186" dyno= connect= service= status=503 bytes= protocol=https
我在这里附加我的app.js
require("dotenv").config();
const express = require("express");
const app = express();
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const findOrCreate = require("mongoose-findorcreate");
const timestamp = require("mongoose-timestamp");
const MongoStore = require('connect-mongo');
const auth = require("./routes/auth");
const User = require("./database/models/user_model");
const blogRoute = require("./routes/blogRoute");
mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.set("useCreateIndex", true);
mongoose.set("useFindAndModify", false);
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(session({
secret: "foo",
saveUninitialized: false,
resave: false,
store: MongoStore.create({
mongoUrl: process.env.DB_URI,
mongoOptions: { useUnifiedTopology: true },
collectionName: 'sessions',
autoRemove: 'native',
})
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(User.createStrategy());
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3001/auth/google/positate" || "https://positate.herokuapp.com/auth/google/positate",
},
function (accessToken, refreshToken, profile, cb) {
User.findOrCreate(
{
googleId: profile.id,
name: profile.displayName,
username: profile.emails[0].value,
image: profile.photos[0].value,
},
function (err, user) {
return cb(err, user);
}
);
}
)
);
app.get("/", (req, res) => {
res.render("Landing");
});
app.use("/", auth);
app.use("/blog", blogRoute);
app.use("/", blogRoute);
app.use("/category", blogRoute);
app.listen(3001 || process.env.PORT, '0.0.0.0', () => {
console.log("Server is running.");
});
这是我的配置文件
web: node app.js
这是我的Package.json
{
"name": "positate",
"version": "1.0.0",
"description": "",
"main": "app.js",
"engines": {
"node": "14.15.4",
"npm": "6.14.10"
},
"scripts": {
"start": "node app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"connect-mongo": "^4.4.1",
"dotenv": "^10.0.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"express-session": "^1.17.2",
"mongoose": "^5.12.11",
"mongoose-findorcreate": "^3.0.0",
"mongoose-timestamp": "^0.6.0",
"passport": "^0.4.1",
"passport-google-oauth20": "^2.0.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^6.1.0"
}
}
我尝试了针对此错误的各种解决方案,但都无法解决。在过去的三天里,我一直在努力解决这个问题。
请帮我解决这个错误
提前感谢您。
解决方案
问题在于您定义端口的方式,它始终在3001
上运行,这在Heroku上是不可能的,您需要绑定$PORT
环境变量。
更改您的代码以首先检查是否定义了process.env.PORT
(它将位于Heroku上,但在您的本地开发环境中将默认为3001)
app.listen(process.env.PORT || 3001, '0.0.0.0', () => {
console.log("Server is running.");
});
参见NodeJS on Heroku
相关文章