【问题标题】:_http_server.js:192 throw new RangeError(`Invalid status code: ${statusCode}`);_http_server.js:192 throw new RangeError(`无效的状态码:${statusCode}`);
【发布时间】:2017-01-22 20:57:31
【问题描述】:

这是我的代码:

var express = require('express');
var http = require('http');
var redis = require('redis');
var url = require('url');
var client = redis.createClient().setMaxListeners(0);

var app = express();
app.set('port', 3000);

app.get('/*', function(req, res) {
  var key = url.parse(req.url).pathname;
  client.on('connect', function() {
    console.log('connected to redis!');
  });
  client.get(key, function(err, reply) {
    if( reply == null) {
      client.set(key, 1);
      client.expire(key, 300);
      res.send('1');
    }
    else {
      client.incr(key, function(err, reply) {
        console.log('increment value: ' + reply);
        res.sendStatus(reply);
      });
    }
  });

});


http.createServer(app).listen(app.get('port'), function() {
  console.log('listening');
});

这是我运行文件时的输出 ($ node test.js): 我在我的 ubuntu 机器上试过这个,它完美地工作。这就是我在我的mac上得到的。有人可以解释一下为什么会这样。任何帮助将不胜感激。

听 增量值:2 _http_server.js:192 throw new RangeError(`无效的状态码:${statusCode}`); ^ RangeError:无效的状态代码:2 在 ServerResponse.writeHead (_http_server.js:192:11) 在 ServerResponse._implicitHeader (_http_server.js:157:8) 在 ServerResponse.OutgoingMessage.end (_http_outgoing.js:559:10) 在 ServerResponse.send (/Users/sharath/webapps/docker/node_modules/express/lib/response.js:209:10) 在 ServerResponse.sendStatus (/Users/sharath/webapps/docker/node_modules/express/lib/response.js:346:15) 在 Command.callback (/Users/sharath/webapps/docker/test.js:24:13) 在 normal_reply (/Users/sharath/webapps/docker/node_modules/redis/index.js:714:21) 在 RedisClient.return_reply (/Users/sharath/webapps/docker/node_modules/redis/index.js:816:9) 在 JavascriptRedisParser.returnReply (/Users/sharath/webapps/docker/node_modules/redis/index.js:188:18) 在 JavascriptRedisParser.execute (/Users/sharath/webapps/docker/node_modules/redis-parser/lib/parser.js:415:12)

【问题讨论】:

    标签: node.js http express redis


    【解决方案1】:

    Http 响应状态应该是整数。不能是字符串、对象、数组等,应该从 100 开始。

    从您的代码中我看到您尝试这样做

    res.sendStatus(reply);

    检查回复变量。从 redis incr 响应我认为它是字符串“OK”。

    这很糟糕..所以要修复它只需使用

    res.sendStatus(reply ? 200 : 500);

    也检查一下。

    http://expressjs.com/en/4x/api.html#res.sendStatus

    还有这个

    https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

    编辑

    如果您需要将一些 JSON 或数据发送到前端,请这样做

    res.json({thisIsMyNumber: reply});

    res.send({thisIsMyNumber: reply});

    希望这会有所帮助。

    【讨论】:

    • 该值为2,从错误消息中可以明显看出。这是有道理的,因为它们是从初始值 1 递增的。
    • en.wikipedia.org/wiki/List_of_HTTP_status_codesstars from 100..只需将回复更改为 200...或 400 或其他...但不少于 100
    • 实际上我正在实现一个非常简单的访客计数器。在这里,我试图计算 URI 聚合的过去 5 分钟内的访问次数。我在 ubuntu 上尝试了相同的代码,我得到了正确的输出。即 1 2 3 ...最近五分钟的访问次数。但是在我的 Mac 上,我收到了这个错误。如果这不是正确的方法,我如何在我的页面上显示这个数字?很抱歉,这是我第一次使用 node。
    • @MykolaBorysyuk 我知道,但我想说的是reply 的值是2,而不是'OK' 或其他任何值。
    • 你可以尝试使用 res.sendStatus(101) 代替 res.sendStatus(reply)
    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    相关资源
    最近更新 更多