I am new to Web Development & have started building a MERN app as a side project. I am encountering the below problem.
When I am trying to access a route in my routes file, I was unable to access it. It is showing this error in Postman when trying to access it :
GET http://localhost:4000/api/matches/matchlist
Error: connect ECONNREFUSED ::1:3000
Request Headers
User-Agent: PostmanRuntime/7.31.1
Accept: */*
Postman-Token: fa77215b-eb3c-4667-8b8d-63ffc2bd84be
Host: localhost:4000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
My server file :
const express = require('express');
const connectMongDB = require('./config/connectMongoDB');
// Connecting the matches.js file to the server.js file
const matches = require('./routes/matches');
// MongoDB connection
connectMongDB();
const app = express();
app.use(express.json());
const port = process.env.PORT || 4000;
// Using matches middleware for matches management routes
app.use('/api/matches', matches);
app.listen(port, ()=>{
console.log(`Server is running on port: ${port}`);
});
My matches routes file :
const express = require('express');
const router = express.Router();
const Match = require('../database_models/matchModel');
// Get all matches
router.get('/', async (req, res) => {
//console.log("User is authenticated");
try {
const matches = await Match.find();
console.log(matches);
res.json(matches);
}
catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
});
// Get match by id
router.get('/:id', ensureUserAuthenticated, async (req, res) => {
try {
const match = await Match.findById(req.params.id);
if (!match) {
return res.status(404).json({ msg: 'Match not found' });
}
res.json(match);
}
catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
});
router.get('/matchlist', (req,res) => {
res.send("Success");
});
module.exports = router;
I was able to access the route '/api/matches/' but getting the above said error when trying to access '/api/matches/matchlist/' route.
And it also showing Internal Server Error(500) in the console box.
Can someone please tell me what's going wrong.