Home > Blockchain >  Only GET '/' route is working in my MERN project, testing with POSTMAN getting 404 respons
Only GET '/' route is working in my MERN project, testing with POSTMAN getting 404 respons

Time:09-17

My app.js looks like this:

const express = require('express');
const connectDB = require('./config/db');
var cors = require('cors');

const fruits = require('./routes/api/fruits');

const app = express();

connectDB();

app.use(cors({origin: true, credentails: true}));

app.use(express.json({extended: false}));

app.get('/', (req, res) => res.send('Hello World'));

app.use('/api/fruits', fruits);

const port = process.env.PORT || 8082;

app.listen(port, () => console.log(`Server running on port ${port}`));

My fruit.js in routes/api/fruits.js

const express = require('express');
const router = express.Router();

const Fruit = require('../../models/Fruit');

router.get('/test', (req, res) => {
    res.send('fruit route testing!');
    console.log('Route found');
});

router.get('/', (req, res) => {
    Fruit.find()
        .then(fruits => res.json(fruits))
        .catch(err => res.status(404).json({nofruitsfound : 'No Fruit Found'}));
});

router.get('/:id', (req, res) => {
    Fruit.findById(req.params.id)
        .then(fruit => res.json(fruit))
        .catch(err => res.status(404).json({nofruitsfound : 'No Fruit Found'}));
});

router.post('/', (req, res) => {
    Fruit.create(req.body)
        .then(fruit => res.json({ mgs: 'Fruit added sucessfully'}))
        .catch(err => res.status(400).json({ error: 'Unable to add this fruit'}));
});

router.put(':/id', (req, res) => {
    Fruit.findByIdAndUpdate(req.params.id, req.body)
        .then(fruit => res.json({ mgs: 'Updated successfully'}))
        .catch(err => res.status(400).json({ error: 'Unable to update the Database'}));
});

router.delete('/:id', (req, res) => {
    Fruit.findByIdAndDelete(req.params.id, req.body)
        .then(fruit => res.json({ mgs: 'Fruit deleted sucessfully'}))
        .catch(err => res.status(404).json({ error: 'Unable to find fruit by Id and delete'}));
});

module.exports = router;

http://localhost:8082/ gets "Hello World"

http://localhost:8082/api/fruit/test gets:

<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /test</pre>
</body>
</html>

I have also tried http://localhost:8082/api/fruits/test is working but POST for http://localhost:8082/api/fruits/ is getting {"error":"Unable to add this fruit"}

postman request for POST http://localhost:8082/api/fruits/

CodePudding user response:

you have neither /test or /api/test defined. /api/fruit/test would be the closest you have with the endpoints with a :id parameter.

Be aware though you have a . character where your express app is consuming your /api/fruit route. It says /api/fruit.

CodePudding user response:

Based on this line app.use('/api/fruits.', fruits); you have defined a base path for the endpoints/routes in the fruits.js file. What this means is that for you to be able access the routes you have to prefix the base path to the url as follows:

http://localhost:8082/api/fruits/test - will get the /test route

http://localhost:8082/api/fruits - will get all fruits as defined in fruits.js

http://localhost:8082/api/fruits - used with a POST request you can create a new fruit.

Your base path plus host is then this: http://localhost:8082/api/fruits

Looks like you are new to Express.js so I am going to add this:

app.use(express.json({extended: false})); - This line tells express to handle request body in JSON only. So in Postman or any other HTTP client you are using, ensure the body for your POST requests are JSON.

  • Related