Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3683

Postman GET Request hangs and never returns any results

$
0
0

I am attempting to Build A REST API With Node.js, Express, and MongoDB. Everything seem to work so far. The server starts and connects to the database. The database (named API) has two collections (Articles and Sources). I am trying to send GET request to the Articles collection. However, when I send the request, Postman sort of hangs and then gives this an error and crashes the server:

Could not get any responseThere was an error connecting to localhost:3000/articles.Why this might have happened:The server couldn't send a response:Ensure that the backend is working properlySelf-signed SSL certificates are being blocked:Fix this by turning off 'SSL certificate verification' in Settings > GeneralProxy configured incorrectlyEnsure that proxy is configured correctly in Settings > ProxyRequest timeout:Change request timeout in Settings > General

The server throws the following error(s) when it crashes:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 0x10007e891 node::Abort() [/usr/local/Cellar/node/13.8.0/bin/node] 2: 0x10007e9c0 node::OnFatalError(char const*, char const*) [/usr/local/Cellar/node/13.8.0/bin/node] 3: 0x10017e6ab v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/usr/local/Cellar/node/13.8.0/bin/node]

My code looks like the following:

require('dotenv').config()const express = require('express') const app = express()const mongoose = require('mongoose')mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true , useUnifiedTopology: true })const db = mongoose.connectiondb.on('error', (error) => console.error(error))db.once('open', () => console.log('Connected to Database'))app.use(express.json())const articlesRouter = require('./routes/articles')app.use('/articles', articlesRouter)app.listen(3000, () => console.log('Server Started'))

My routes; I'm attempting to send GET request to get All the articles in the database (that are in the Articles collection):

const express = require('express')const router = express.Router()const Article = require('../models/articles')//Getting All Articlesrouter.get('/', async (req, res) => {  try {      const articles = await Article.find()      res.json(articles)  } catch (err){    res.status(500).json({message: err.message})  }})//Getting One Articlerouter.get('/:id', (req, res) => {})module.exports = router

If I change the router.get to this router.get('/articles', async (req, res) Postman will no longer hang, but throw the following error:

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Error</title></head><body><pre>Cannot GET /articles</pre></body></html>

And my routes:

const mongoose = require('mongoose')const articlesSchema = new mongoose.Schema({    author: String, content: String, description: String, publishedAt: Date, source_id: String, summarization: String, title: String, url: String, urlToImage: String}, { collection: 'Articles'});module.exports = mongoose.model('article', articlesSchema)

As I mentioned in the beginning, this is my first attempt at building a REST API. Any help you can provide to help me figure out why a Postman GET request isn't going through, will be greatly appreciated.


Viewing all articles
Browse latest Browse all 3683

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>