Debian 上运行的 Node.js 服务日志中,HTTP 状态码用于指示请求在客户端与服务器之间的处理结果。状态码为三位数字,按首位划分为 1xx–5xx 五类,含义固定且与语言/框架无关。
| 状态码 | 含义 | 常见触发 | 排查要点 |
|---|---|---|---|
| 200 | OK | 正常请求成功 | 正常业务路径 |
| 201 | Created | POST/PUT 创建资源成功 | 检查创建结果是否落库 |
| 204 | No Content | 成功但无响应体 | 确认客户端是否预期无内容 |
| 301 | Moved Permanently | 资源永久迁移 | 更新站点地图与反向代理配置 |
| 302 | Found | 临时跳转 | 检查登录/地域跳转逻辑 |
| 304 | Not Modified | 协商缓存命中 | 核对 ETag/Last-Modified 与缓存策略 |
| 400 | Bad Request | 参数缺失/格式错误 | 校验请求体/查询参数/Content-Type |
| 401 | Unauthorized | 未认证或凭证无效 | 检查 Authorization 头与认证中间件 |
| 403 | Forbidden | 权限不足 | 核对访问控制与角色授权 |
| 404 | Not Found | 路由或资源不存在 | 检查路由表与静态资源路径 |
| 405 | Method Not Allowed | 方法不被允许 | 核对路由允许的 GET/POST/PUT/DELETE |
| 408 | Request Timeout | 客户端请求超时 | 检查客户端超时设置与网络质量 |
| 413 | Payload Too Large | 请求体过大 | 调整 body-parser/反向代理体积限制 |
| 414 | URI Too Long | URL 过长 | 缩短查询串或改用 POST |
| 415 | Unsupported Media Type | 不支持的媒体类型 | 设置正确的 Content-Type(如 application/json) |
| 500 | Internal Server Error | 未捕获异常/代码错误 | 查看堆栈与异常日志,补齐 try/catch 与中间件错误处理 |
| 502 | Bad Gateway | 网关/上游无效响应 | 检查 Nginx/负载均衡 与上游健康 |
| 503 | Service Unavailable | 过载/维护 | 检查进程存活、限流/熔断与维护页 |
| 504 | Gateway Timeout | 网关/上游超时 | 调整反向代理与上游超时阈值 |
| 以上状态码定义与常见触发场景适用于 Node.js(含 Express) 应用,在 Debian 环境下通过 Nginx/PM2/系统日志 观察时含义一致。 |
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('OK');
});
server.listen(3000, () => console.log('Server on 3000'));
app.get('/ok', (req, res) => res.status(200).send('OK'));
app.get('/created', (req, res) => res.status(201).json({ id: 1 }));
app.use((err, req, res, next) => {
console.error(err); // 便于 Debian 系统日志/服务日志排查
res.status(500).send('Internal Server Error');
});
access_log /var/log/nginx/access.log;),在 Node.js 统一使用 Morgan 或自定义中间件输出 statusCode、method、url、responseTime、ip,便于在 journald/syslog 中检索与告警。