Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 请求模块为,导致express.js设置头_Node.js_Express - Fatal编程技术网

Node.js 请求模块为,导致express.js设置头

Node.js 请求模块为,导致express.js设置头,node.js,express,Node.js,Express,我将节点与express和request模块一起使用。在开始使用request从另一台服务器获取信息之前,我可以调用res.json。但是,当我试图在请求中的回调函数中使用res.json时,我会收到错误消息,消息头已经发送 一种解决方案是将格式头显式设置为'application/json',但是我不想打开res.json。还有其他解决办法吗?这是不是因为express认为没有设置头,所以擅自发送一个头 代码示例: ` router.get('/app/:action',函数(req,res)

我将节点与express和request模块一起使用。在开始使用request从另一台服务器获取信息之前,我可以调用
res.json
。但是,当我试图在请求中的回调函数中使用
res.json
时,我会收到错误消息,消息头已经发送

一种解决方案是将格式头显式设置为
'application/json'
,但是我不想打开
res.json
。还有其他解决办法吗?这是不是因为express认为没有设置头,所以擅自发送一个头

代码示例: ` router.get('/app/:action',函数(req,res){


`

正如您所说,您收到的错误如下
标题已经发送。

让我简单地给你解释一下

您必须根据您的情况从多个位置编写
res.json

出现此错误是因为
res.json
正在执行多次

当它第一次执行时,它不会给您错误,但第二次它会给您错误,因为响应已经发送

你的代码有一些循环漏洞。请调试它

让我试着用这里的例子来详细解释你们

const express = require('express');
const app = express();

//
//  To generate error
//
app.get('/generate_error', function(req, res) {

    //
    //  When check_error query param is true, It will throw you error else
    //  all good
    //
    if (req.query.check_error) {

        //
        //  check_error is true so let's send response first here.
        //
        res.send('Hello World!');

    }

    //
    //  If check_error is true then it will try to send response again for
    //  same request.
    //  If check_error is false then it will by send response first time
    //  here so no error
    //
    res.send('Hello World!');

});

//
//  Solution 1 for above case
//
app.get('/ignore_error_1', function(req, res) {

    if (req.query.check_error) {

        res.send('Hello World!');

    } else {

        res.send('Hello World!');

    }

});

//
//  Solution 2 but different with solution 1
//
app.get('/ignore_error_2', function(req, res) {

    if (req.query.check_error) {

        //
        //  When you don't have anything to execute after sending response
        //  then just add return here.
        //  In other case you have to manage your code as per your
        //  requirement if you have anything needs to be executed after
        //  sending response from multiple places.
        //  NOTE : Make sure you will not execute res.json multiple times
        //
        return res.send('Hello World!');

    }

    return res.send('Hello World!');

});

app.listen(3000, function() {

    console.log('Example app listening on port 3000!')

});
只需执行这三个get URL

/generate_error?check_error=true
/ignore_error_1?check_error=true
/ignore_error_2?check_error=true

正如您所说,您收到的错误如下
标题已经发送。

让我简单地给你解释一下

您必须根据您的情况从多个位置编写
res.json

出现此错误是因为
res.json
正在执行多次

当它第一次执行时,它不会给您错误,但第二次它会给您错误,因为响应已经发送

你的代码有一些循环漏洞。请调试它

让我试着用这里的例子来详细解释你们

const express = require('express');
const app = express();

//
//  To generate error
//
app.get('/generate_error', function(req, res) {

    //
    //  When check_error query param is true, It will throw you error else
    //  all good
    //
    if (req.query.check_error) {

        //
        //  check_error is true so let's send response first here.
        //
        res.send('Hello World!');

    }

    //
    //  If check_error is true then it will try to send response again for
    //  same request.
    //  If check_error is false then it will by send response first time
    //  here so no error
    //
    res.send('Hello World!');

});

//
//  Solution 1 for above case
//
app.get('/ignore_error_1', function(req, res) {

    if (req.query.check_error) {

        res.send('Hello World!');

    } else {

        res.send('Hello World!');

    }

});

//
//  Solution 2 but different with solution 1
//
app.get('/ignore_error_2', function(req, res) {

    if (req.query.check_error) {

        //
        //  When you don't have anything to execute after sending response
        //  then just add return here.
        //  In other case you have to manage your code as per your
        //  requirement if you have anything needs to be executed after
        //  sending response from multiple places.
        //  NOTE : Make sure you will not execute res.json multiple times
        //
        return res.send('Hello World!');

    }

    return res.send('Hello World!');

});

app.listen(3000, function() {

    console.log('Example app listening on port 3000!')

});
只需执行这三个get URL

/generate_error?check_error=true
/ignore_error_1?check_error=true
/ignore_error_2?check_error=true

请您提供一些代码好吗?请注意res.json也会结束请求。您不能在请求函数中发送两次响应,因此请发布一段代码片段,以便我们检查。因此,在添加
break
后,问题似乎得到了解决,因此我假设我在
default
中添加的看似无害的代码确实有问题与标题有关的内容(如果我有权访问完整的错误日志,我应该看到来自
default
之后的行的错误消息,但遗憾的是我没有/没有完整的访问权限)。您能提供一些代码吗?请注意res.json也会结束请求。您不能在请求函数中发送两次响应,因此请发布一段代码片段,以便我们可以检查。因此,添加
break
后,问题似乎得到了解决,因此我假设我在
default
中添加的看似无害的代码确实存在与标题有关(如果我有权访问完整的错误日志,我应该看到错误消息来自
default
之后的行,但遗憾的是我没有/没有完整的访问权限)。这个问题确实与标题有关。在我添加了break before
default
,它调用了一个函数,而该函数反过来又试图重置标题。我认为这不仅值得一提您在回答中提到的内容(即
res.json
是标题设置和发送的包装器),但也值得一提的是,
break
应该总是在案例结束后添加,无论代码看起来多么无害。@andrew Yeah我同意你的观点,如果你从每个
开关
案例发送响应,则必须添加
break
。我刚才详细解释了你。我注意到你已经提到了
break。希望它有意义。问题确实与标题有关。在我添加了break before
default
(默认值之前的break)后,问题得到了解决,这是在调用一个函数,而该函数又试图重置标题。我认为这不仅值得一提您在回答中提到的内容(即
res.json
是头设置和发送的包装器),但也值得一提的是,
break
应该总是在案例结束后添加,无论代码看起来多么无害。@andrew Yeah我同意你的观点,如果你从每个
开关
案例发送响应,则必须添加
break
。我刚才详细解释了你。我注意到你已经提到了
break有问题。希望有意义。