error.js 457 B

123456789101112131415
  1. module.exports = (err, req, res, next) => {
  2. const statusCode = err.statusCode || 500;
  3. const isProduction = process.env.NODE_ENV === 'production';
  4. const response = {
  5. error: isProduction ? '服务器错误' : err.message,
  6. ...(!isProduction && { stack: err.stack })
  7. };
  8. // 记录完整错误日志
  9. console.error(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
  10. console.error(err);
  11. res.status(statusCode).json(response);
  12. };