Node not calling the correct URL, API works fine in POSTMAN

I have a few APIs: This one works correctly: 1) //GET TIMELINE POSTS router.get("/timeline/:userId", async … Read more Node not calling the correct URL, API works fine in POSTMAN

I have a few APIs:

This one works correctly:

1)
//GET TIMELINE POSTS
router.get("/timeline/:userId", async (req, res) => {

    try {
        const currentUser = await User.findById(req.params.userId);
        const userPosts = await Post.find({ userId: currentUser._id });
        
        const friendPosts = await Promise.all(
            currentUser.following.map( friendId => {
                return Post.find({ userId: friendId });
            })
        );

        res.status(200).json(userPosts.concat(...friendPosts));

    } catch( err ) {
        res.status(500).json(err);
    }

});

2)
//GET User's all POSTS
router.get("/profile/:username", async (req, res) => {

    try {
        
        const user = await User.findOne({username: req.params.username});
        const posts = await Post.find({userId: user._id});
        
        res.status(200).json(posts);
        
    } catch( err ) {
        res.status(500).json(err);
    }

});

This line calls the apis:

const res = username 
            ?  await axios.get("posts/profile/" + username) 
            : await axios.get("posts/timeline/6120b0e09308d933dd1c");

When username is not provided the timeline api is called which works fine both in app and postman.
The url for the timeline api is:

http://localhost:8000/api/posts/timeline/6120b0e09308d933dd1c

But when I need to get posts for the user, the correct api which works fine in postman is:

http://localhost:8000/api/posts/profile/matt

but when the api is called from the app code for which is mentioned above, it adds extra words to the url, here is what I see in VSCode when it is running:

:ffff:127.0.0.1 - - [14/Oct/2021:06:28:20 +0000] "GET /api/profile/posts/profile/matt HTTP/1.1" 404 169

it should go to /api/posts/profile/matt but instead it goes to the above mentioned one

Why is that the 1st api works fine but the 2nd one adds extra words to the URL?

Source: React – Stack Overflow



Leave a Reply

Your email address will not be published. Required fields are marked *