Why GET request is not working in Node.js?

 const express = require('express');
 const mongoose = require ("mongoose");
 const app = express();
 const Student = require('../models/students');
 require('dotenv').config();
 const PORT = process.env.PORT || 3000;
 const cors = require('cors');
 app.use(express.json());
 app.use(express.urlencoded({extended:true}));
 app.use(cors());
 app.use(express.static('./dist/mean-stack-application'));


 mongoose.connect(process.env.MONGO_URL, {
   useCreateIndex: true,
   useNewUrlParser: true,
   useUnifiedTopology: true,
   useFindAndModify: false
 }).then(() => {
    console.log("Connection is Successful");
 }).catch((e) => {
    console.log("No Connection");
 });


 app.get("/studentData", async( req,res ) => {
    try{
       const getStudentsData = await Student.find();
       console.log(getStudentsData);
       res.send(getStudentsData);
   }catch(e){
      res.send(e);
   }
 });

 app.listen(PORT, () => {
 console.log(`Connection is setup at ${PORT}`);
 });

Angular Code

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment'

@Injectable({
   providedIn: 'root'
})
export class RestoService {

constructor(private http: HttpClient) { }

 readonly baseUrl = 'https://mean-curd-app.herokuapp.com';

 readonly url = '/studentData';


 getList(){
    return this.http.get(`${this.baseUrl}${this.url}`)
 }
}

It’s working on localhost 3000 but when I host this MEAN stack app on Heroku then I got error msg which is status code 304. Connection is successful but something is wrong that’s why I got this error msg.

GitHub Source Code Link : https://github.com/SwapnilVedpathak-sv/mean-stack-application
Heroku Hosted Link : https://mean-curd-app.herokuapp.com

14 thoughts on “Why GET request is not working in Node.js?”

Leave a Comment