问题
大家好,不幸的是,我在部署我的Node.js Express和MongoDB应用程序到Heroku后遇到了一个问题。该应用程序在我的计算机上工作得很好,但我在Heroku日志中收到了H12错误。下面是供参考的错误:
2021-08-19T16:20:26.186995+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=accp-clean-blog-app.herokuapp.com request_id=222fe564-d711-4b62-b13c-272290bf0de5 fwd="174.246.20.114" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
我已经做了什么
我在这里广泛搜索了堆栈溢出并尝试了多种解决方案…起初我认为这是一个MongoDB连接错误,我没有添加我的MongoDB url到Heroku上的config vars
。所以我改变了,我仍然收到相同的错误。我有我的MongoDB url和端口与process.env
符号。我也有一个档案与web: node index.js
。下面是我的index.js
:
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload');
const expressSession = require('express-session');
const newPostController = require('./controllers/newPost')
const homeController = require('./controllers/home')
const storePostController = require('./controllers/storePost')
const getPostController = require('./controllers/getPost')
const validateMiddleware = require('./middleware/validationMiddleware')
const newUserController = require('./controllers/newUser')
const storeUserController = require('./controllers/storeUser')
const loginController = require('./controllers/login')
const loginUserController = require('./controllers/loginUser')
const authMiddleware = require('./middleware/authMiddleware')
const redirectIfAuthenticatedMiddleware = require('./middleware/redirectIfAuthenticatedMiddleware')
const logoutController = require('./controllers/logout')
const flash = require('connect-flash');
const app = new express()
const ejs = require('ejs')
app.set('view engine', 'ejs');
//app.use denotes Express.js middleware
//Middleware are functions that Express executes in the order they are declared and they
//can make changes to the request and response objects
//When a request comes in to the server, Express will execute all of the app.use middleware
//on the request, before completing the request and sending the response.
app.use(express.static('public'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}))
app.use(expressSession({
secret: 'keyboard cat'
}))
app.use(fileUpload);
app.use('/posts/store', validateMiddleware)
app.use(flash());
//GET requests simply resources/retrieves the requested information from the server
//POST requests are used to request an addition to the server, such as a blog entry, a photo,
//signing up for an account, or buying an item. POST is used to create records on servers.
app.get('/', homeController)
app.get('/post:id', getPostController)
app.get('/posts/new', authMiddleware, newPostController);
app.post('/posts/store', authMiddleware, storePostController)
app.get('/auth/register', redirectIfAuthenticatedMiddleware, newUserController)
app.post('/users/register', redirectIfAuthenticatedMiddleware, storeUserController)
app.get('/auth/login', redirectIfAuthenticatedMiddleware, loginController)
app.post('/user/login', redirectIfAuthenticatedMiddleware, loginUserController)
app.get('/auth/logout', logoutController)
app.use((req,res) => res.render('notfound'));
//the global variable logged in is accessible to all of the EJS files. This will make
//modifications to the navigational bar when a user is logged in
global.loggedIn = null;
app.use("*", (req,res,next) => {
loggedIn = req.expressSession.userId;
next();
})
let port = process.env.PORT;
if(port == null || port == ""){
port = 4000;
}
app.listen(port, () => {
console.log('App listening on port: ' + port)
})
/*const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URL, {useNewUrlParser: true, useUnifiedTopology: true})
.then(() => console.log('MongoDB Connected...'))
.catch((err) => console.log(err))
*/
const db = process.env.MONGODB_URL;
const connectDB = async () => {
try {
await mongoose.connect(db, {
useUnifiedTopology: true,
useNewUrlParser: true
});
console.log("MongoDB is Connected...");
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
//mongoose.connect('mongodb://localhost/my_database', {useNewUrlParser: true});
很明显,我在这里错过了一些东西…我只是看不出来提前感谢你的帮助:)
你也可以在这里找到这个项目的GitHub存储库
和这里的Heroku应用
我也遇到了同样的问题。它已经为我去了,当我改变了连接字符串与那些驱动程序版本2.2.12或更高(不开始与mongodb+srv:)。