Express在发送后无法设置标头

时间:2016-11-13 15:07:56

标签: json node.js express mongoose

我试图在这个问题上找到类似的问题,虽然有很多相同的错误,但我并不认为我出于同样的原因得到错误。

我有一个路由,POST / api / servers,它在数据库中创建一个服务器列表。我不知道在快速路线中包含如此多的逻辑是否是错误的,但它似乎一直在努力,直到我不断得到这个错误。

基本上,如果创建一个已经存在的服务器(地址+端口组合),那么路由会发回一个错误,其中包含“服务器已经存在”的错误。作为JSON中的消息,但是当我尝试创建邮局中已经存在的服务器时,如果发生这种情况,我会按预期恢复JSON,但是我的服务器在发送后会设置标头崩溃'

router.post('/servers', passport.authenticate('jwt', { session: false }), function(req, res) {
    //This is very lazy validation, to do in future version, create error object
    // which accumulates the problems with the form and can return all problems at
    // once to user 
    if (!req.body.name) {
        return res.json({success: false, message: 'NAME REQUIRED'})
    }
    if (!req.body.address) {
        return res.json({success: false, message: 'ADDRESS REQUIRED'})
    }
    if (!req.body.image) {
        return res.json({success: false, message: 'IMAGE REQUIRED'})
    }
    if (!req.body.description) {
        return res.json({success: false, message: 'DESCRIPTION REQUIRED'})
    }

    var newServer = new Server();
    newServer.name = req.body.name;
    newServer.address = req.body.address;
    //if a custom port is specified, uses it otherwise defaults to 25565
    if (req.body.port) {
        newServer.port = req.body.port;
    } else {
        newServer.port = 25565;
    }

    //Check if server with address and port not already exists
    Server.findOne({ address: newServer.address, port: newServer.port }, function(err, server) {
        if (err) {
            return res.json({success: false, message: 'Internal Error'});
        } else if (server) {
            return res.json({success: false, message: 'SERVER ALREADY EXISTS'});
        }
    });

    newServer.image = req.body.image;
    newServer.description = req.body.description;
    newServer.owner = req.user._id; // Gives the current user id

    if (req.body.votifier_enabled) {
        //If votifier is enabled on this server
        newServer.votifier.enabled = true;
        if (!req.body.votifier_address) {
            return res.json({success: false, message: 'VOTIFIER ADDRESS REQUIRED'})
        }
        //if custom votifier port specified, it is used otherwise defaults to 8192
        if (req.body.votifier_port) {
            newServer.votifier.port = req.body.votifier_port;
        } else {
            newServer.votifier.port = 8192;
        }
        if (!req.body.votifier_pubkey) {
            return res.json({success: false, message: 'VOTIFIER PUBLIC KEY REQUIRED'})
        }
        newServer.votifier.address = req.body.votifier_address;
        newServer.votifier.pubkey = req.body.votifier_pubkey;

        //Check if votifier server with address and port not already exists
        Server.findOne({ votifier: { address: newServer.votifier.address, port: newServer.votifier.port } }, function(err, server) {
            if (err) {
                return res.json({success: false, message: 'Internal Error'});
            } else if (server) {
                return res.json({success: false, message: 'VOTIFIER ALREADY EXISTS'});
            }
        });
    };

    if(!req.body.hidden) {
        newServer.hidden = false;
    } else {
        newServer.hidden = req.body.hidden;
    };

    console.log('passed all verification');

    //Lastly: Verify that the server really does belong to this user,
    //checks that string config.VERIFY_OWNERSHIP_STRING is in servers motd
    mcping(req.body.address, req.body.port, function(err, stats) {
        if (err) {
            //Problem connecting to server
            return res.json({success: false, message: 'SERVER CONNECTION FAILED: Is your server online?'})
        } else {
            console.log('pinged server successfully')
            var status = stats;
            if (status.description.text.includes(config.VERIFY_OWNERSHIP_STRING)){
                newServer.status.online = true;
                newServer.status.motd = status.description.text;
                newServer.status.currPlayers = status.players.online;
                newServer.status.maxPlayers = status.players.max;
            //Verification successful, server does belong to this user
                //Validation complete and newServer object created! Now to save it to database
                newServer.save(function(err, server) {
                    console.log('attempted to save the server');
                    if (err) {
                        //Theoretically - this should not happen unless theres an internal error
                        console.log(err.errors);
                        return res.json({success: false, message: err});
                    } else if (server) {
                        console.log('save successful')
                        return res.json({success: true, _id: server._id});
                    }
                });
            } else {
                return res.json({success: false, message: "SERVER VALIDATION FAILED: Is '" + config.VERIFY_OWNERSHIP_STRING + "' in your motd?"});
            }
        }
    }, 3000);
})

这是导致问题的路由,我认为问题是因为即使服务器已经存在,所以JSON消息被发送回来说SERVRE ALREADY EXISTS仍然调用下面的所有console.logs,包括猫鼬保存功能。 我以为res.json应该结束请求响应周期?所以我觉得有这么多res.jsons打电话给我的印象是好的,但现在我认为这是问题

感谢您提供任何帮助,并对令人困惑的代码感到抱歉!

0 个答案:

没有答案