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 response
There 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 properly
Self-signed SSL certificates are being blocked:
Fix this by turning off 'SSL certificate verification' in Settings > General
Proxy configured incorrectly
Ensure that proxy is configured correctly in Settings > Proxy
Request 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.connection
db.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 Articles
router.get('/', async (req, res) => {
try {
const articles = await Article.find()
res.json(articles)
} catch (err){
res.status(500).json({message: err.message})
}
})
//Getting One Article
router.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.
Your code seems correct. One thing that i had encounter is in your route you specify only
`router.get('/', async (req, res) =>{})`
and you are calling api as
"localhost:3000/articles
" which is incorrect.
you haven't specify any route as
router.get('/articles', async (req, res) =>{})
.
so postman not able to find such route and respond none.
Try with route which i have specify above or call only "localhost:3000/
" as you specify in routes.
Other thing you can do is just put following line in your app.js file and no need to change anything else in your code.
app.use('/articles', articlesRouter)
Hope this help you out.