使用 Express 和 HTTPS.get 或 HTTPS.request 的最佳方式
The best way to use Express and HTTPS.get or HTTPS.request
我正在尝试使用一个简单的应用程序.js(node.js)在Micro BOSH上进行REST调用。这涉及通过端口 25555 上的 HTTPS 调用 url,并进行身份验证,并且不验证 SSL 证书。我找到了通过HTTPS.request
或HTTPS.get
获取 JSON 结果的方法。
问题是我是,我想使用快递作为Web服务器。现在,我在执行这两项操作时都遇到了以下错误(我的https.request
是app.get('/', function(req, res) {...})
之外函数的一部分:
Error: Can't remove headers after they are sent.
at ServerResponse.OutgoingMessage.removeHeader (http.js:718:11)
at ServerResponse.send (/Users/dev/bes/bes-rest/node_modules/express/lib/response.js:179:10)
at /Users/dev/bes/bes-rest/app.js:95:7
at Layer.handle [as handle_request] (/Users/dev/bes/bes-rest/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/dev/bes/bes-rest/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/dev/bes/bes-rest/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/dev/bes/bes-rest/node_modules/express/lib/router/layer.js:82:5)
at /Users/dev/bes/bes-rest/node_modules/express/lib/router/index.js:235:24
at Function.proto.process_params (/Users/dev/bes/bes-rest/node_modules/express/lib/router/index.js:313:12)
at /Users/dev/bes/bes-rest/node_modules/express/lib/router/index.js:229:12
我感觉因为我使用快速组合与 http(创建服务器)然后 https 进行 REST 调用,所以应用程序以这种方式做出反应。但是,在使用 Express 时进行 REST 调用如何是最好的呢?
这是我用于所需的:
var express = require('express');
var app = express();
var server = require('http').Server(app);
var request = require('request');
var path = require('path');
var https_er = require('https');
感谢您对此的任何提示。
这是我用来执行 REST 调用的代码:
var _deployments = {};
function getDeployments() {
var options = {
host: resturl,
port: restport,
path: '/deployments',
method: 'GET',
rejectUnauthorized: false,
requestCert: true,
agent: false,
headers: {
'Authorization': 'Basic ' + new Buffer(rest_uname + ':' + rest_pword).toString('base64')}
};
var req = https_er.get(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
// Buffer the body entirely for processing as a whole.
var bodyChunks = [];
res.on('data', function(chunk) {
// You can process streamed parts here...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
console.log('BODY: ' + body);
_deployments = body;
})
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
这是到目前为止的应用程序:
app.get('/', function(req, res) {
res.send('Hello!');
res.send(JSON.parse(_deployments)); // this is the line 95
});
server.listen(port);
不能有两个"res.send"语句。
// one of these two statements has to go
res.send('Hello!');
res.send(JSON.parse(_deployments));
您可以执行以下操作:-
app.get('/', function(req, res) {
res.send({"_deployments":JSON.parse(_deployments)});
});server.listen(port);
相关文章:
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 有条件更新d3.js力图中节点的最佳方法
- 为react组件传递道具的最佳方式
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 让Webpack管理Quirky AMD定义的最佳方式
- 在承诺链中处理早期回报的最佳方式
- 将jQuery.ech()方法转换为本地JavaScript抽象的最佳方法是什么
- 关于引入外部javascript文件的问题&css通过https
- Angularjs 1.5.x本地化最佳实践
- 处理浮点错误的最佳方法是什么
- jquery ajax https调用给出ERR_INSECURE_RESPONSE
- javascript导入的最佳实践是什么
- MobileFirst:在客户端运行计时器作业-最佳选项
- 在ng重复循环中显示条件内容的最佳方式是什么
- 在phonegap中为android调用onload函数的最佳方式
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- 从数组中删除元素的最佳方法是:javascript/jquery
- 如何通过安全的https连接在javascript中使用基于soap xml的Web服务
- 链接两个网页或网络应用程序的最佳方式
- 使用 Express 和 HTTPS.get 或 HTTPS.request 的最佳方式