简体   繁体   English

使用两个快速应用程序时出现CORS问题

[英]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). 我正在尝试在Windows Server 2012上使用两台Node.js Express服务器,每台服务器具有不同的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. 这些应用程序是两个Watson Chatbot,因此它们都需要使用路由/会话与IBM进行通信。

One chatbot uses port 443 and the other one use 8443. 一个聊天机器人使用端口443,另一个聊天机器人使用端口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. 问题是:每个服务器都位于不同的目录中,并且有自己的子目录“ public”,但是当我执行两个服务器时,使用端口8443的服务器使用端口443服务器的“ public”子目录。

  • Chatbots 聊天机器人

    • certificates 证书
    • Chatbot1 Chatbot1

      • node_modules node_modules

      • public 上市

      • css CSS
      • script 脚本
    • Chatbot2 Chatbot2
      • node_modules node_modules
      • public 上市
      • css CSS
      • script 脚本

Chatbot1 app.js: 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: 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? 如何“强制”服务器使用其自己的子目录?

"Problem" solved. “问题解决了。

Actually, it was my lack of study about how FQDN actually works and a little to blame on Anti-virus. 实际上,这是我缺乏有关FQDN实际工作方式的研究,而且还应归咎于防病毒。

example2.b.br don't need the ":443" on its url, because the port is default for HTTPS. example2.b.br的网址中不需要“:443”,因为该端口是HTTPS的默认端口。 But when I use example1.b.br, it needs ":8443" after ( https://example1.b.br:8443 ). 但是当我使用example1.b.br时,它在( https://example1.b.br:8443 )后需要“: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. 在端口上创建仅通过Intranet进行通信的异常后,问题得以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM