Search code examples
javascriptnode.jsexpresscorswatson-conversation

CORS Problem when using two express application


I'm trying to use two Node.js express servers on a Windows Server 2012, each one with a different FQDN (example1.b.br | exemple2.b.br). The applications are two Watson Chatbots, so both of them need to use route /conversation to communicate with IBM.

One chatbot uses port 443 and the other one use 8443.

The problem is: Each one of them are in different directories and have their own subdirectory called 'public', but when I execute both servers, the one using port 8443 uses the port 443 server's 'public' subdirectory.

  • Chatbots

    • certificates
    • Chatbot1

      • node_modules

      • public

      • css
      • script
    • Chatbot2
      • node_modules
      • public
      • css
      • script

Chatbot1 app.js:

const AssistantV1 = require('watson-developer-cloud/assistant/v1');
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const https = require('https');
var fs = require('fs');
var httpApp = express();

var workspace;

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

const app = express();

app.use(bodyParser.json());
app.use(express.static('./public'));

const port = 80;
const httpsPort = 8443;

httpApp.set('port', process.env.PORT || 80);

const assistant = new AssistantV1({
  username: 'XXXXX',
  password: 'XXXXX',
  url: 'https://gateway.watsonplatform.net/assistant/api/',
  version: '2018-02-16'
});

  workspace = 'XXXXXXX';
  app.post('/conversation/', (req, res) => {
    const { text, context = {} } = req.body;
    const params = {
      input: { text },
      workspace_id: workspace,
      context,
    };

    assistant.message(params, (err, response) => {
      if (err) res.status(500).json(err);

      res.json(response);
    });
  });

try{
  //var httpServer = http.createServer(httpApp, app).listen(port);
  var httpsServer = https.createServer(options, app).listen(httpsPort); 
  //httpServer.listen(port, () => console.log(`Running on port ${port}`));
  httpsServer.listen(httpsPort, 'exemple1.b.br', () => console.log(`HTTPS Running on port ${httpsPort}`));  
  console.log(`---------------------------------`);
  console.log(`-----------ROBO INICIADO---------`);
  console.log(`---------------------------------`);
}catch(err){
  console.log(`*********************************`);
  console.log(`*****Falha ao iniciar o Robo*****`);
  console.log(`*********************************`);
  console.log(err);
} */

Chatbot2 app.js:

const AssistantV1 = require('watson-developer-cloud/assistant/v1');
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const https = require('https');
var fs = require('fs');
var httpApp = express();

var workspace;

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

const app = express();

app.use(bodyParser.json());
app.use(express.static('./public'));

const port = 80;
const httpsPort = 443;

httpApp.set('port', process.env.PORT || 80);

const assistant = new AssistantV1({
  username: 'xxxxxxx',
  password: 'xxxxxx',
  url: 'https://gateway.watsonplatform.net/assistant/api/',
  version: '2018-02-16'
});

  workspace = 'XXXXXXX'
  app.post('/conversation/', (req, res) => {
    const { text, context = {} } = req.body;
    const params = {
      input: { text },
      workspace_id: workspace,
      context,
    };

    assistant.message(params, (err, response) => {
      if (err) res.status(500).json(err);

      res.json(response);
    });
  });


try{
  var httpsServer = https.createServer(options, app).listen(httpsPort); 
   httpsServer.listen(httpsPort, 'exemple2.b.br', () => console.log(`HTTPS Running on port ${httpsPort}`));  
  console.log(`---------------------------------`);
  console.log(`-----------ROBO INICIADO---------`);
  console.log(`---------------------------------`);
}catch(err){
  console.log(`*********************************`);
  console.log(`*****Falha ao iniciar o Robo*****`);
  console.log(`*********************************`);
}

How can I "force" the server to use its own subdirectory?


Solution

  • "Problem" solved.

    Actually, it was my lack of study about how FQDN actually works and a little to blame on Anti-virus.

    example2.b.br don't need the ":443" on its url, because the port is default for HTTPS. But when I use example1.b.br, it needs ":8443" after (https://example1.b.br:8443).

    At least this simple mistake make me learn about this detail.

    After that, I discovered that the server anti-virus were blocking some files. After creating an exception on the port to communicate only through intranet, the problem got solved.