FYB
FYB

Reputation: 19

The best way to use Express and HTTPS.get or HTTPS.request

I am trying to use a simple app.js (node.js) to do REST call on a Micro BOSH. This involve to call url by HTTPS on port 25555 with authentication and with no validation for the SSL certification. I found my way to get the JSON result through HTTPS.request or HTTPS.get.

The thing is that I am and I want to use express as the web server. Right now, I obtained the following error while doing both (my https.request is part of a function outside of the 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

I got the feeling that because I am using express combine with http (that create the server) and then https for the REST call, the app is reacting that way. But how can it be the best to do my REST calls while using Express?

Here is what I am using for the required:

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');

Thank's for any hint on this.

Here is the code I am using to do the REST call:

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();
}

And here is the app.get so far:

app.get('/', function(req, res) {

  res.send('Hello!');
  res.send(JSON.parse(_deployments));  // this is the line 95

});
server.listen(port);

Upvotes: 0

Views: 532

Answers (2)

Praveen
Praveen

Reputation: 113

You can do as following:-

app.get('/', function(req, res) {

res.send({"_deployments":JSON.parse(_deployments)});

}); server.listen(port);

Upvotes: 0

takinola
takinola

Reputation: 1773

You cannot have two "res.send" statements.

  // one of these two statements has to go
  res.send('Hello!');
  res.send(JSON.parse(_deployments));

Upvotes: 1

Related Questions