tinuggunit
tinuggunit

Reputation: 39

Post request to Express.js server in postman gives the following error

When I make this post request on postman -

http://localhost:5000/api/edit/links?editData={"editData"[{"link":"www.github.com/","name":"link name"},{"name":"link name","link":"linkdata"}]}

I get the following error:

<html lang="en">
    
<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>
    
<body>
    <pre>Cannot POST /api/edit/links</pre>
</body>
    
</html>

My server Index file looks like this:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const cors = require('cors');
require('dotenv').config();
require('./db/connectDB');

//Import routes
const authRoute = require('./routes/auth');
const editRoute = require('./routes/edit');
const publicRoute = require('./routes/public');

//Middleware
app.use(express.json());

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

//Route Middlewares
app.use(cors());
app.use('/api/user', authRoute);
app.use('/', publicRoute);

const PORT = 5000;

app.listen(PORT, () => console.log('server is up and running '));

The route I'm trying to reach is in this file

edit.js

const router = require('express').Router();
const verify = require('./verifyToken');
const {editLinksValidation} = require('../validation');
const ObjectId = require('mongoose').Types.ObjectId;
const User = require('../model/User');
const fetchFavicon = require('@meltwater/fetch-favicon').fetchFavicon;

router.post('/api/edit/links/:editData', verify, (req, res) => {

    const editData = JSON.parse(req.params.editData);

    User.updateOne(
        {_id: id},
        {
            $set:
            {
                links: editData
            }
        }
    ).then(metadata => res.send(metadata))
     .catch(err => console.log(err));

});

What am I doing wrong here?

Upvotes: 0

Views: 42

Answers (2)

Christian Fritz
Christian Fritz

Reputation: 21384

As the error says, there is no route defined for /api/edit/links for the POST method. And indeed your code shows that your route is /api/edit/links/:editData -- that last path element cannot be empty. Not sure what your intention was with that, but I have the hunch that your intention was to get the value from the query instead, in which case you'd want to use:

router.post('/api/edit/links/', verify, (req, res) => {
  const editData = JSON.parse(req.query.editData);

Upvotes: 1

Anatoly
Anatoly

Reputation: 22803

You don't use imported editRoute in the server index file at all. I suppose you should add this line:

app.use('/', editRoute);

Upvotes: 1

Related Questions