【发布时间】:2014-07-18 06:00:37
【问题描述】:
我尝试使用 https/http 请求服务器并在网页中显示结果。 它在服务器上作为脚本工作,但失败,我通过 get 请求返回结果。
var express = require('express');
var app = express();
var port = 1337;
var https = require('https');
app.get('/', function(req, response, next) {
doRequest(function(resp){
response.send("response" + resp); // FAIL ON REQUEST !
});
});
function doRequest(callback){
var post_data"query=test";
var post_options = {
host: 'mySite.com',
path: '/path/to/source',
method: 'POST',
secureProtocol: 'SSLv3_method'
};
// Set up the request
var post_req = https.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
callback(chunk);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
doRequest(console.log); // WORKS !
我收到此错误:
http.js:707
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:707:11)
at ServerResponse.res.set.res.header (/node_modules/express/lib/response.js:564:10)
at ServerResponse.res.contentType.res.type (/node_modules/express/lib/response.js:434:15)
at ServerResponse.res.send (/node_modules/express/lib/response.js:114:43)
我使用 Express 4 和节点 v0.10.15。
【问题讨论】:
标签: javascript node.js http express