Home > Mobile >  my program is not responding to any request get nor post but database is correctly connected and eve
my program is not responding to any request get nor post but database is correctly connected and eve

Time:04-16

const express = require("express")
const app =express()
const mongoose =require("mongoose");
const cors=require('cors');
const FeedbackModel=require('./models/feedback')
app.use(express.json());
app.use(cors);
mongoose.connect("the url no prblm here")
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
})
  

the get method is working fine before after i was working on my frontend it stoped working

app.get("/getfeedback",(req,res)=>{
FeedbackModel.find({},(err,result)=>  {
        if(err)
        {res.json(err)
        }
        else
        {res.json(result)

        }
    });
 })

the app is working fine but if i call for any request the url keep on loading without any response it just keeps on spinning

app.post("/addfeedback",async(req,res)=>{
const feedback=req.body;
const newFeedback= new FeedbackModel(feedback);
await newFeedback.save();
res.json(feedback);
})
app.listen(3001,()=>{`enter code here`
console.log("Server runs in port 3001");
}); 

CodePudding user response:

You are using cors package incorrectly.

It has to be called and then it returns whatever value is necessary to the middleware.

Imagine a function that returns a function, the first has to be called to reach the second one.

Try this

app.use(cors());
  • Related