EXPRESS - Some API endpoints are not found despite valid request - node.js

I have a simple Reddit clone project written in express. I noticed that some endpoints are throwing errors despite having a correct URL. Here is my code.
App.js
app.use("/api", router);
Router
router.route("/register")
.post(UsersController.APIregisterUser);
router.route("/login")
.post(UsersController.APIlogin);
router.route("/posts/") // Fetch all posts
.get(PostsController.APIgetPosts)
.post(Utility.verifyToken, PostsController.APIaddPost);
router.route("/comment/submit/:id/") // /posts/comment/123456789
.put(Utility.verifyAndPassData, PostsController.APIaddComment);
router.route("/posts/vote")
.put(Utility.verifyAndPassData, PostsController.APIcastVote);
router.route("/posts/:id/") // req.params.id
.get(PostsController.APIgetPostByID)
.delete(Utility.verifyToken, PostsController.APIdeletePost);
router.route("/posts/categories")
.get(PostsController.APIgetCategories);
When I try to fetch all posts by going to http://localhost:8080/api/posts (GET) via Insomnia, it works as intended. However, when I try to post a comment by going to http://localhost:8080/api/comment/submit/*post's id* (PUT) , it says 404 not found. I checked the PostsController.APIaddComment if the error is in there but there seems to be no error on that part. I even tried replacing the whole function with a console.log("test") to see if the endpoint is being reached but none happens.
Here is the PostsController.APIaddComment:
static async APIaddComment(userData, req, res, next) {
try {
const commentBody = {
user: req.body.username,
body: req.body.body,
date: new Date()
}
const addComment = await PostsDAO.addComment(commentBody, req.params.id);
res.status(200).json({
status: "Comment submitted!",
addComment
});
} catch(e) {
res.status(400).json({
error: `Error in PostsController APIaddComment: ${e}`
})
}
}
Other than that particular function, everything works as intended. What could be the possible cause?

Related

I cant get a response from a POST request with an array in the body, Using NodeJS / Axios and Postman

This is a course quiz and this is the most basic information I need in order to create a React app. But while the endpoint URL is correct, the page "/products" returns a "400" error when I try to request the product list. The instructions I'm given are:
Obtain a list of products with
Route: /products
Body: Array
method: POST
{
"product-codes": [
"98798729",
"84876871",
"29879879",
]
}
My index.js
...
app.post(`/products`, async (req, res) => {
try {
const response = await axios.post(`${apiURL}/products`);
// console.log(response.data);
res.status(200).json(response.data);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
in Postman
I use http://localhost:4000/products
and pass a Body / Raw /JSON:
{
"product-codes": [
"98798729",
"84876871",
"29879879",
]
}
But I can't get in! I am not seeing something obvious because this is the entry point to a very long and complex quiz. Thanks
What I see from the code is a recursive long call.
app.post(`/products`, async (req, res) => {
try {
const response = await axios.post(`${apiURL}/products`); // calling the same end point
// console.log(response.data);
res.status(200).json(response.data);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
You should do something like this:
app.post(`/products`, async (req, res) => {
// do your logic
// when you pass body from postman on this endpoint
// you will receive the body here and save it to DB
// or do the manipulation and then send back the response
res.status(200).json(req.body.data);
});
I highly recommend you to first follow through some tutorials to understand how API works and how to create simple API using Node.js.

Getting "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" even though I only have one res.render()

I am using Mongoose and Express/Node.js to build a simple api, but when I try to click on the "Read More" link (which uses Express routing parameters), I get "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client". I understand that this happens when multiple responses are sent for a single post, but I can't for the life of me find where this is happening.
My code is below:
Post.find({}, function(err, foundPosts) {
res.render("home", {homeStartingContent: homeStartingContent, posts: foundPosts});
res.redirect("/");
});
})
app.get("/compose", function(req, res) {
res.render("compose");
})
app.post("/compose", function(req, res) {
const post = new Post({
title: req.body.title,
body: req.body.newPost,
teaser: req.body.newPost.substring(0,99) + "...",
});
// save the post and refresh home page to display most recent post
post.save(function(err) {
if(!err) {
res.redirect("/");
}
});
});
// express routing parameters; uses whatever comes after : to decide what to do
app.get("/posts/:postId", function(req, res) {
const requested = _.lowerCase(req.params.postId);
Posts.findOne({_id: requested}, function(err, post) {
res.render("post", {post: post});
});
});```
I'm pretty sure the issue is in the last app.get("/posts/:postID"...), but I can't figure it out.
I understand that this happens when multiple responses are sent for a single post
It also happens when you send more headers after already having sent a response. And that's what you do by calling first res.render and then res.redirect near the top of your code snippet. Also, this does not make sense, because the redirection will prevent the user from reading what you rendered before.

Why am I getting a "400: Bad Request" but the request successfully posts to my DB

I am not sure what is happening. I had all the same code setup in dev using localhost and everything was working fine. I hosted my app to Vercel and my API to heroku and from what I can tell the API is working perfectly. The issue now is when I make a post request I get a 400 Bad Request error in the browser but it still makes the request and posts to my DB I have setup. Any help can be appreciated. I built this application in a MERN stack with NEXT.js
Here is my client side Post request
const postSubmit = async e => {
e.preventDefault();
try {
const { data } = await axios.post('/create-post', { content, image });
if(data.error) {
toast.error(data.error);
} else {
setPage(1);
newsFeed();
toast.success("Post created");
setContent('');
setImage({});
// socket.emit('new-post', data);
}
} catch(e) {
console.log(e);
}
}
Here is my server side handling of the post request
export const createPost = async (req, res) => {
const { content, image } = req.body;
if(!content.length) {
return res.json({
error: 'Content is required'
})
}
try {
const post = new Post({ content, image, postedBy: req.user._id });
await post.save();
const postWithUser = await Post.findById(post._id).populate('postedBy', '-password -secret');
res.json(postWithUser);
} catch (e) {
console.log(e);
res.sendStatus(400);
}
};
And here is what the browser is telling me
Chrome Browser Info
This is most likely caused by a typo on the MongoDB connection string (URI), similar to this answer.
In the linked answer, the typo was the semi-colon in &w=majority;. In your case, the value is somehow majorityDATABASE=, so double-check your MongoDB connection string and try to remove the extra DATABASE= in the write concern parameter.
It's likely that the await Post.findById() call is not finding a result which could be throwing an error when you try to call res.json(postWihUser). That would then cause an error when postWithUser is not defined.
What is logged to the server's terminal when an error occurs? That should help you identify the problem.

Express is not reading the request body

I am new in node and express. So, I was learning how to do post request. When I learned, it was alright and it was creating another document in mangodb database. Today when I tried to do the same, this problem occurred.
I tried different things like removing required part in schema. But it seems express is failing to read req.body since when I send the request without any required fields in schema, it happily accepted and filled up default fields. I don't really understand what is happening. I was parallelly doing another project which is not giving this error.
PS. I am using mongoose#5.
Code to create tour:
exports.createTour = async (req, res) => {
try {
const newTour = await Tour.create(req.body);
res.status(201).json({
status: 'success',
data: {
tour: newTour,
},
});
} catch (err) {
res.status(404).json({
status: 'fail',
message: err.message,
});
}
};
If the content-type for your POST is JSON, then you need this:
app.use(express.json())
as middleware before your POST request handler. That will read the JSON body of the request, parse it and put it in req.body.

request body is undefined when trying to POST in express

For some reason, request body is undefined when trying to make a post request:
here is my router:
router.route("/").post(schoolController.createSchool);
here is what I put in schoolController for createSchool:
exports.createSchool = async (req, res) => {
try {
console.log(req.body);
// return undefined
const newSchool = await School.create(req.body);
res.status(201).json({
status: "success",
data: {
school: newSchool,
},
});
} catch (err) {
res.status(400).json({
status: "fail",
message: err,
});
}
};
adding on, I am following jonas's nodejs course on udemy, and he has almost the exact thing as this, except its for handling tour requests instead of school
The problem you are facing here is likely that you have not configured the body-parser middleware correctly. The normal req will not contain any property by the name body. Only once the request passes through the body-parser middleware, the body key will be added to the req. You can try console logging req. If the request is logged correctly, it is more than likely that you need to look into configuring you bodyparser middleware correctly before you can use req.body in your code.

Resources