gpt4 book ai didi

javascript - Node js : HTTP POST : Error: Can't set headers after they are sent

转载 作者:行者123 更新时间:2023-12-03 11:42:06 26 4
gpt4 key购买 nike

我正在制作一个 GCM 应用程序,所以当我需要服务器来匹配公平时。

有一段Node js编写的代码。

var http = require('http');
var express = require('express');
var app = express();
app.use(express.static('public'));
app.use(express.bodyParser());
app.use(app.router);
var gcm = require('node-gcm');
var message = new gcm.Message();
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
key1: '안녕하세요.',
key2: 'saltfactory push demo'
}
});

var server_access_key = '~';
var sender = new gcm.Sender(server_access_key);
var registrationIds = [];

var registration_id = '~-~-~-~-~~~-~';
// At least one required
registrationIds.push(registration_id);

app.post('/enroll',function(request,response){
var my_number = Number(request.param('myphonenumber'));
var another_number = Number(request.param('anotherphonenumber'));

console.log(my_number);
console.log(another_number);

response.send(my_number);
response.send(another_number);
response.json({success:1});
response.json({success:"my_number"});
response.json({success:"another_number"});

});

http.createServer(app).listen(52273,function(){
console.log('Server Running');
});

当我在 POST-MAN 执行 POST 时,我在浏览器中什么也没有得到我的终端显示此错误代码:

   NaN
NaN
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
at ServerResponse.res.setHeader (/Users/~)
at ServerResponse.res.set.res.header (/Users/~)
at ServerResponse.res.send (/Users/~)
at ServerResponse.res.json (/Users/~)
at /Users/~
at callbacks (/Users/~)
at param (/Users/~)
at pass (/Users/~)
at Router._dispatch (/Users/~)

NaN 是由

引起的
console.log(my_number);
console.log(another_number);

我不知道为什么打印NaN。我显然插入了 key :我的电话号码、另一个电话号码和值:12345678、98765432。

最佳答案

您不能多次使用response.json或response.send方法

 app.post('/enroll/:myphonenumber/:anotherphonenumber',function(request,response){
var my_number = Number(request.params.myphonenumber);
var another_number = Number(request.params.anotherphonenumber);
console.log(my_number);
console.log(another_number);
response.json({'my_number':my_number,'another_number':another_number,success:1});
});

关于javascript - Node js : HTTP POST : Error: Can't set headers after they are sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26232429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com