【问题标题】:Configure HTTPS on NodeJS for Watson Conversation在 NodeJS 上为 Watson 对话配置 HTTPS
【发布时间】:2017-01-19 05:54:50
【问题描述】:

这是示例中提供的代码:

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

但是当使用 https 而不是服务器时,它不能很好地与 IBM Watson 对话代码一起使用。 以下是我使用的代码:

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

【问题讨论】:

    标签: node.js ibm-watson chatbot watson-conversation


    【解决方案1】:

    在这种情况下,Express API 文档非常清楚地说明了这一点。 这个article 也可以提供帮助。

    您可以在node.js 中创建一个HTTPS

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

    Express 文档显示:

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多