Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exam1 dhlieu2 #67

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
20 changes: 5 additions & 15 deletions src/controllers/Blog.controller.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const getBlogs = async (req, res) => {
blogs
});
} catch (error) {
json.status(500).json({
message: error.message
});
next(err)
};
};

Expand All @@ -26,9 +24,7 @@ const getBlog = async (req, res) => {
blog
});
} catch (error) {
res.status(500).json({
message: error.message
});
next(err)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next(error)

};
};

Expand All @@ -47,9 +43,7 @@ const createBlog = async (req, res) => {
newBlog
});
} catch (error) {
res.status(500).json({
message: error.message
});
next(err)
};
};

Expand All @@ -68,9 +62,7 @@ const updateBlog = async (req, res) => {
updatedBlog
});
} catch (error) {
res.status(500).json({
message: error.message
});
next(err)
};
};

Expand All @@ -84,9 +76,7 @@ const deleteBlog = async (req, res) => {
}
res.status(204)
} catch (error) {
res.status(500).json({
message: error.message
});
next(err)
};
};

Expand Down
7 changes: 7 additions & 0 deletions src/middleware/error.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const error = (err, req, res, next) => {
res.status(err.status || 500).json({
message: err.message
});
}

module.exports = error
3 changes: 2 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express')
const dotenv = require('dotenv')
const mongoose = require('mongoose')
const routes = require('./routers')
const errorMiddleware = require('./middleware/error.middleware')
const app = express()

dotenv.config()
Expand All @@ -15,7 +16,7 @@ mongoose
.catch((err) => {
console.log(err);
});

app.use(errorMiddleware)
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});