The /
route works fine and any other route that does not use async/await
seems to work, but when I go to
router.get('/posts', (req, res) => {
Post.find({})
.then((data) => {
res.json(data);
})
.catch((err) => console.log(err));
});
It doesnt return any data and I get a "Request timed out error" in Heroku
I checked the .env
file variables and tried using them on the localhost:3000
and it works.
Here is the full routes file:
const express = require('express');
const router = express.Router();
const User = require('../models/user');
const Post = require('../models/posts');
const path = require('path');
router.get('/test', (req, res) => {
res.send('Working');
}); //This route works
router.get('/posts', (req, res) => {
Post.find({})
.then((data) => {
res.json(data);
})
.catch((err) => console.log(err));
}); //This one gives me an error
module.exports = router;
The posts mongoose model:
const mongoose = require('mongoose');
const postSch = mongoose.Schema({
title: {
required: true,
type: String,
},
content: {
type: String,
},
file: String,
});
const Post = mongoose.model('Post', postSch);
module.exports = Post;
The main app.js
file:
require('dotenv').config();
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const mainRoute = require('./routes/mainRoutes');
const cors = require('cors');
const mongoose = require('mongoose');
const passport = require('passport');
const session = require('express-session');
const ejs = require('ejs');
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.render('index.ejs');
});
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.use(
session({
secret: process.env.COOKIE_SECRET,
resave: false,
saveUninitialized: false,
secure: false,
// maxAge: 24 * 60 * 60 * 1000 * 7,
// originalMaxAge
})
);
app.use(cors());
app.use(express.json({ extended: true }));
app.use(express.urlencoded({ extended: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use('/', mainRoute);
require('./passportconfig')(passport);
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
the .env
file:
MONGO_URI="mongodb+srv://<account_name>:<password>@cluster0.f7aej.mongodb.net/web?retryWrites=true&w=majority"
COOKIE_SECRET="somerandomstring"