我正在尝试使用mongo DB ATlas进行AWS数据部署。现在,在这里,我正在尝试使用日志记录工具来实现可靠的错误处理系统。除了“无法获得任何响应”错误(我与mongoDB的连接失败时出现此错误)之外,我还介绍了大多数错误情况
我将在路由文件app.use(errorHandler);
的末尾提供此处理程序,而在next(new Error("Could not complete this request"));
路由功能的末尾提供此处理程序。
const app = express();
const errorHandler = require('./../routes-error-handler/routesError');
exports.signupAUser = (req, res, next) => {
const user = new User({
_id: mongoose.Types.ObjectId(),
firstName: req.body.firstName,
lastName: req.body.lastName,
role: req.body.role,
email: req.body.email,
password: req.body.password
});
user.save()
.then(response => {
res.status(201).json({
message: 'User created successfully'
})
})
.catch(error => {
res.status(500).json({
message: 'Failed to create user',
error
})
});
next(new Error("Could not complete this request"));
}
routesError.js:
exports.errorHandler = (error, req, res, next) => {
fs.appendFile('log.txt', `Not Found! Timed out response! Please try at a later time ${error}`, (err) => {
if (err) throw err;
});
res.json({
message: 'Not Found! err Timed out!',
})
}
请让我知道一种实现“无法获得任何响应”错误处理的好方法。