var express = require('express');
var router = express.Router();
var http = require('http');
router.get('*', function(req, res, next) {
http.get("http://steamcommunity.com/id/reminant/inventory/json/730/2",
function (response) {
response.on('data', function (data) {
var stringData = data.toString();
console.log(stringData);
res.render('index', { "data": stringData });
});
}).on('error', function (error) {
console.log(error);
});
});
控制台:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
at ServerResponse.header (e:\Users\Documents\Repositories\CSGOTest\node_modules\express\lib\response.js:718:10)
at ServerResponse.contentType (e:\Users\Documents\Repositories\CSGOTest\node_modules\express\lib\response.js:551:15)
at ServerResponse.send (e:\Users\Documents\Repositories\CSGOTest\node_modules\express\lib\response.js:138:14)
at done (e:\Users\Documents\Repositories\CSGOTest\node_modules\express\lib\response.js:957:10)
at Object.exports.renderFile (e:\Users\Documents\Repositories\CSGOTest\node_modules\jade\lib\index.js:374:12)
at View.exports.__express [as engine] (e:\Users\Documents\Repositories\CSGOTest\node_modules\jade\lib\index.js:417:11)
at View.render (e:\Users\Documents\Repositories\CSGOTest\node_modules\express\lib\view.js:126:8)
at tryRender (e:\Users\Documents\Repositories\CSGOTest\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (e:\Users\Documents\Repositories\CSGOTest\node_modules\express\lib\application.js:591:3)
答案 0 :(得分:0)
你的代码应该这样写:
var buffers = [];
var size = 0;
response.on('data', function (data) {
buffers.push(data);
size += data.length;
});
res.render('index', {data: Buffer.concat(buffers, size).toString()});
答案 1 :(得分:0)
对字符串进行连接响应并在结束处理程序中调用呈现
var stringData = '';
response.on('data', function(data) {
stringData += data.toString();
}
response.on('end', function() {
res.render('index', { "data": stringData });
}