我正在制作一个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的印刷原因。我显然插入了密钥:myphonenumber,anotherphonenumber&值:12345678,98765432。
答案 0 :(得分:1)
您不能多次使用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});
});