microsoft / hack-together-teams

HackTogether: The Microsoft Teams Global Hack | Register, Hack, Win 👇
https://aka.ms/hack-together-teams/register
MIT License
455 stars 37 forks source link

Connecting communication between the tab app and the bot #51

Closed irwinwilliams closed 1 year ago

irwinwilliams commented 1 year ago

Hi there, I'm trying to get requests from my tab app to be processed by the bot. However, I'm being stymied by CORS, any suggestions to get around this? The tab portion is hosted on localhost:53000 whereas the bot is on localhost:3978 This is the error:

Access to fetch at 'http://localhost:3978/api/notify' from origin 'https://localhost:53000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Update: Here's what I've tried:


import cors from 'cors';
//...other set up code
// Configure CORS
const corsOptions = {
  origins: ['*']
};
const corsMiddleware = cors(corsOptions);

// Set up CORS
server.use((req, res, next) => {
  // Allow requests from any origin
  res.setHeader('Access-Control-Allow-Origin', '*');
  // Allow the following HTTP methods
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  // Allow the following headers
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  // Allow credentials (if needed)
  res.setHeader('Access-Control-Allow-Credentials', 'true');

  next();
});

server.use(cors({

  origin: true,

  methods: 'GET, POST, OPTIONS, PUT, DELETE, PATCH',

  allowedHeaders: 'Origin, Content-Type, Accept, Authorization, X-Request-With, Content-Range, Content-Disposition, Content-Description',

  credentials: true

  }));

Update 2: I set the mode to no-cors when making the request. Doesn't fix the issue but unblocks me for the moment.

Obelixx commented 1 year ago

If you host them on the same port, you can "skip" this problem. There will be no issue to serve the API from http://localhost:3978/api/ and the tab from http://localhost:3978/

aycabas commented 1 year ago

Thanks @Obelixx for helping out. @irwinwilliams is this issue fixed on your end? You can also ask your questions here: https://github.com/microsoft/hack-together-teams/discussions