节点js错误 – 发送后无法发送标头

我正在使用ebay-api作为node-js,当我刷新页面时出于某种原因,我得到一个错误。 在控制台日志中,我收到“发送后无法发送标题”错误。 这里是我的代码,你能弄清楚为什么我在刷新时“在发送之后发送头文件”?

// example simple request to FindingService:findItemsByKeywords var ebay = require('../index.js'); var http = require('http'); var express = require('express'); var app = express(); var io = require('socket.io'); app.set('port', process.env.PORT || 5000); app.get('/getEbay', function (req, res) { console.log('inside get'); // for avoiding crossbrowser-error res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); res.header('Content-Type', 'application/json'); var params = {}; params.keywords = ["cat"]; params['paginationInput.entriesPerPage'] = 10; ebay.ebayApiGetRequest({ serviceName: 'FindingService', opType: 'findItemsByKeywords', appId: 'MYAPPID', // FILL IN YOUR OWN APP KEY, GET ONE HERE: https://publisher.ebaypartnernetwork.com/PublisherToolsAPI params: params, // filters: filters, parser: ebay.parseItemsFromResponse // (default) }, // gets all the items together in a merged array function ebayApiGetRequest(error, items) { if (error) throw error; console.log('Found', items.length, 'items'); // res.send(items); console.log(JSON.stringify(items)); res.contentType('application/json'); res.send(JSON.stringify(items)); // } } ); }); http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); console.log('Listening on port 5000...'); 

我认为我的错误在于调用函数的顺序,或者有办法避免这个错误的调用?

而不是这个:

 res.send(JSON.stringify(items)); res.send(items); 

尝试:

 res.end(JSON.stringify(items)); 
 function ebayApiGetRequest(error, items) { if (error) throw error; console.log('Found', items.length, 'items'); // res.send(items); console.log(JSON.stringify(items)); res.contentType('application/json'); res.send(JSON.stringify(items));// you send here res.send(items);//and you send again right afterwards? // } } 

如果你真的执行了两次发送,那么你当然会发送两次数据。 那些res.send()就足够了。

好人,我们找出了问题!

这是正在工作的新代码:

 // example simple request to FindingService:findItemsByKeywords var http = require('http'); var express = require('express'); var app = express(); var io = require('socket.io'); var ebay = require('../index.js'); app.set('port', process.env.PORT || 5000); app.get('/getEbay', function(req, res) { console.log('inside get'); // for avoiding crossbrowser-error res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); res.header('Content-Type', 'application/json'); getEbaybyResults(function(error, items) { if (error) throw error; console.log('Found', items.length, 'items'); // res.send(items); console.log(JSON.stringify(items)); // res.contentType('application/json'); // console.log(try); // res.send(JSON.stringify(items)); res.end(JSON.stringify(items)); }); }); function getEbaybyResults(callback) { var params = {}; params.keywords = ["cat"]; params['paginationInput.entriesPerPage'] = 10; ebay.ebayApiGetRequest( { serviceName: 'FindingService', opType: 'findItemsByKeywords', appId: 'MYAPPID', // FILL IN YOUR OWN APP KEY, GET ONE HERE: https://publisher.ebaypartnernetwork.com/PublisherToolsAPI params: params, // filters: filters, parser: ebay.parseItemsFromResponse // (default) }, function(error, items) { callback(error, items); } ); //ebayApiGetRequest(); } http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); //console.log('Listening on port 5000...');