Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

express - Getting a "Request timed out" error after deploying to Heroku with node.js

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...