Home > OS >  I receive no response from the requests i send in NodeJS API it just keeps loading
I receive no response from the requests i send in NodeJS API it just keeps loading

Time:09-21

my code was perfectly working a couple of days ago and it suddenly stopped working it's connected to the mongodb cluster but i fail to receive response from the database everytime i send a request it's i tried reinstalling node reinstalling mongoose updating all packages but nothing seemed to work

keeps loading forever

and no response when i cancel it

here's the server.js code :

const express = require('express');

const bodyParser = require('body-parser');

const morgan = require('morgan');

const mongoose = require('mongoose');

const cors = require('cors')

require('dotenv/config');


const authJwt = require('./helpers/jwt')

const errorHandler = require('./helpers/error-handler')

const api = process.env.URL;


mongoose.connect(process.env.DATABASE,
    {
        useNewUrlParser:true,
        useUnifiedTopology:true
    })
.then(()=>{
    console.log('connected to database')
})
.catch((err)=>{
    console.log(err)
})


//variables

const app = express();
const port = 9090

//middleware calls

app.use(bodyParser.json());
app.use(morgan('tiny'));
app.use(express.Router())
//app.use('')
app.use(cors());
app.options('*',cors())
app.use(errorHandler)
app.use(authJwt)




const categoriesRouter = require('./routers/categories')
const productsRouter = require('./routers/products')
const ordersRouter = require('./routers/orders')
const usersRouter  = require('./routers/users')
//Routers

app.use(`${api}/categories`,categoriesRouter)
app.use(`${api}/products`,productsRouter)
app.use(`${api}/users`,usersRouter)


app.listen(port,(req,res)=>
{
    console.log('server is running in port '  port )
})

here's one of the routers code :

const {Category} = require('../models/category')

const express = require('express');


const router = express.Router();



router.get('/',async(req,res)=>{
    const categoryList = await Category.find();

    if(!categoryList)
    {
        res.status(500).json({success:false})
    }
    res.status(200).send(categoryList);
})


router.get('/:id',async(req,res)=>{
    const category = await Category.findById(req.params.id)


    if(!category)
    {
        res.status(500).json({message:'The category with the given ID'})
    }

    res.status(200).send(category)
})

router.post('/',async(req,res)=>{
    let category = new Category({
        name:req.body.name,
        icon:req.body.icon,
        color:req.body.color
    })

    category = await category.save();

    if(!category)
    return res.status(404).send('the fag category cannot be created')

    res.send(category)
})


router.delete('/:id', (req,res)=>{
    Category.findByIdAndRemove(req.params.id).then(category=>{
        if(category)
        {
            return res.status(200).json({success:true,message:'the category is deleted'})
        }
        else
        {
            return res.status(404).json({success:false,message:'the category is not found'})
        }
    }).catch(err=>{
        return res.status(400).json({success:false , error: err})
    })
})

router.put('/:id',async (req,res)=>{

    const category = await Category.findByIdAndUpdate(
        req.params.id,
        {
            name:req.body.name,
            icon:req.body.icon,
            color:req.body.color
        },
        //i want to return the new updated data
        { new:true }
    )
    
    if(!category)
    {
        return res.status(400).send('The category cannot be created!');
    }
    res.send(category);
    
})

module.exports = router;

just to let you know it was working a couple of days ago and now it just suddenly stopped working if there's anything i can do or if you've faced the same problem before please reach out

CodePudding user response:

Make sure to send a proper response on the api side of code.

In the case that u are using the express framework, it could look something like this:

router.get('/', (req, res) => {
    res.status(200).json({
        your: data
    })
})

CodePudding user response:

Postman is set to do a GET request, but I think it might be intended to be a POST request when looking at your routers module.

Can you please double check that ?

  • Related