Harsimran Singh
Harsimran Singh

Reputation: 738

Configure HTTPS on NodeJS for Watson Conversation

This is the code that was provided in the example:

'use strict';

var server = require('./app');
var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;

server.listen(port, function() {
  console.log('Server running on port: %d', port);
});

But when using https instead of server it is not working well with IBM Watson conversation code. The below is the code I used:

var https = require('https');
var fs = require('fs');
var server = require('./app');
var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;

var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

var a = https.createServer(options, function (req, res) {
    server.listen(port, function() {
        console.log('Server running on port: %d', port);
    });

}).listen(port);

Upvotes: 1

Views: 205

Answers (1)

Sayuri Mizuguchi
Sayuri Mizuguchi

Reputation: 5330

In the case, Express API doc spells this out pretty clearly. And this article can help too.

You can create a HTTPS in node.js with:

var express = require('express');  //express for it
var server = require('./app');
var https = require('https');
var http = require('http');
var fs = require('fs');
var port = process.env.PORT || process.env.VCAP_APP_PORT || 443; //example

// from the Node.js HTTPS documentation, almost the same your code.
var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
};

// Create a service (the app object is just a callback).
var app = express();

// Create an HTTP service.
http.createServer(app).listen(80); //you can put the port too.
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(port);

The Express documentation show this:

enter image description here

Upvotes: 3

Related Questions