Added new docs for Back-end#248
Conversation
There was a problem hiding this comment.
Great job, @ajay-dhangar! 🎉 Thank you for submitting your pull request to CodeHarborHub. We appreciate your contribution and enthusiasm! Our team will review it soon. If you have any questions or need further assistance, feel free to reach out. Thanks for contributing!
There was a problem hiding this comment.
Code Review
This pull request replaces placeholder content with comprehensive documentation for backend development, covering Node.js basics, Express.js, routing, middleware, error handling, and the MVC pattern. The review feedback identifies several important improvements, including securing error responses to prevent information leakage in production, managing development dependencies locally, implementing proper error handling in asynchronous controllers and callbacks to prevent process crashes, and using strict equality for route parameter comparisons.
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal Server Error", | ||
| error: err.message // Optional: only show in development | ||
| }); |
There was a problem hiding this comment.
Exposing the error message directly to the client can leak sensitive information about the server's internal state. It is safer to only include detailed error information when the application is running in a development environment.
res.status(500).json({
success: false,
message: "Internal Server Error",
error: process.env.NODE_ENV === 'development' ? err.message : {}
});
| **Install it:** | ||
|
|
||
| ```bash | ||
| npm install -g nodemon |
| exports.getProfile = async (req, res) => { | ||
| const userData = await User.findById(req.params.id); // Ask the Model | ||
| res.json(userData); // Send to the View (Frontend) | ||
| }; |
There was a problem hiding this comment.
The async controller function lacks error handling. If the database call fails, the promise will reject without being caught, which can lead to unhandled promise rejections. It is best practice to wrap the logic in a try...catch block and pass the error to the next middleware.
exports.getProfile = async (req, res, next) => {
try {
const userData = await User.findById(req.params.id);
res.json(userData);
} catch (error) {
next(error);
}
};
| fs.readFile('note.txt', 'utf8', (err, data) => { | ||
| if (err) throw err; | ||
| console.log("File content:", data); | ||
| }); |
There was a problem hiding this comment.
Throwing an error inside an asynchronous callback will cause the Node.js process to crash. Instead of throwing, you should log the error or handle it gracefully to keep the server running.
fs.readFile('note.txt', 'utf8', (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}
console.log("File content:", data);
});
| if (bookId == 1) { | ||
| res.json({ id: 1, title: "The Great Gatsby" }); | ||
| } else if (bookId == 2) { | ||
| res.json({ id: 2, title: "To Kill a Mockingbird" }); | ||
| } else { |
There was a problem hiding this comment.
The bookId variable is a string because it comes from req.params. Using loose equality (==) for comparison with a number relies on type coercion. It is clearer and safer to use strict equality (===) with string literals.
if (bookId === "1") {
res.json({ id: 1, title: "The Great Gatsby" });
} else if (bookId === "2") {
res.json({ id: 2, title: "To Kill a Mockingbird" });
} else {
No description provided.