RangeError nodejs socket.io

RangeError nodejs socket.io

本文关键字:io socket nodejs RangeError      更新时间:2023-09-26

我得到以下错误

RangeError: Maximum call stack size exceeded
    at TCP.hasOwnProperty (native)
    at _hasBinary (/usr/local/lib/node_modules/socket.io/node_modules/has-binary/index.js:49:45)

app.js

var express = require('express');
var app = express();
// var responseHandlerRouter = require('./routes/responseHandlerRouter.js');
routes = require('./routes');
var server = app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});
io = require('/usr/local/lib/node_modules/socket.io').listen(server);
app.use('/', routes(io));

route.js

var express = require('express');
var router = express.Router();

module.exports = function (io) {
    // all of this router's configurations

    router.post('/paymentResponse', function (req, res, next) {
    io.sockets.emit('notification', res);
    res.end('well finally I am here');
});

    return router;
}

index.html

<!doctype html>
<html>
    <head>
        <script src="http://127.0.0.1:3000/socket.io/socket.io.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
        <script>
        var socket = io.connect('http://localhost:3000');
        socket.on('notification', function (data) {
           console.log(data);
        });
        </script>
    </head>
    <body>
        <ul id='messages'></ul>
    </body>
</html>

当我发布到URL时http://localhost:3000/paymentResponse,我在运行nodejs服务器的终端中收到了这个错误。

您正在序列化整个响应对象(res参数),这可能会由于循环引用而导致堆栈溢出。不要序列化整个响应对象,而是仔细挑选所需的属性。