"Unfortunately, SockitIO is repeatedly running into issues with being blocked by

I keep encountering an error in my application (using Node.js as API) and Angular 8 as frontend. The error message says:

Access to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NQJpoTm' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

Here is the server code:

 const io = require('socket.io')(server, {
cors: {
  origin: config.ui_host,
  methods: ['GET', 'POST'],
},

});

And here is the Angular code :

 this.socket = io(socketEndpoint,
  {
    transportOptions: {
      polling: {
        extraHeaders: {
          'Authorization': JSON.parse(localStorage.getItem('token')),
          'Access-Control-Expose-Headers': 'Authorization',
          // 'Accept': 'application/json, text/plain',
          // 'Accept-Encoding': 'gzip, deflate, br',
        }
      }
    },
  }
  );

Answer №1

1- Make sure to check the method being called (it may not be a header issue but a server configuration problem). Ensure that only GET and POST methods are enabled (although it seems like you are using GET). 2- Attempt to clear the node cache. 3- Verify that the value of origin.ui_host is actually localhost:4200.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting up an inline style @Input in Angular 2: A step-by-step guide

I am currently working on a component that needs to display random values, which will be generated randomly and passed through some @Input bindings in the template. Everything seems to be going well, but I am facing an issue when trying to link an @Input t ...

When updating to the latest version of apollo-server-express 2.0.0, it seems that the

Since upgrading, we encountered changes in our code structure. Previously, we utilized the following setup: import express from 'express'; import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'; const app = express() ...

The error message "node Unable to iterate over property 'forEach' because it is undefined" appeared

I am facing an error and unable to find the solution. I believe my code is correct. It is related to a video lesson where I attempt to display popular photos from Instagram using the Instagram API. However, when I try to execute it, I encounter this issue. ...

Importing BrowserAnimationsModule in the core module may lead to dysfunctional behavior

When restructuring a larger app, I divided it into modules such as feature modules, core module, and shared module. Utilizing Angular Material required me to import BrowserAnimationsModule, which I initially placed in the Shared Module. Everything function ...

Access the array values by their respective keys in an object that is returned from a custom JavaScript file utilizing the Node.js file system

I recently came across a config file with a unique format, as shown below: define([], function () { return { productItems: { item1: ['Apple', 'Ball', 'Car'], item2: [&apo ...

The arrow keys (up and down) are unresponsive when using mat-table in an Angular application

There seems to be an issue with my code. When I press the down arrow key for the first time, it goes to the next row as expected. However, when I press the down arrow key again, it does not function properly. (https://i.stack.imgur.com/4qznx.jpg) **HTML* ...

Building an NPM package similar to [name]/[second_name]? The name must consist of URL-friendly characters. How should we include the "[second_name]" in this package?

Can anyone help me understand how to append a package name with something after the slash in NPM? For instance, when creating a package, you typically use: npm init and when specifying the name, it gives an error if I include a "/" character as it restri ...

New alternative for websocket-server in BrowserQuest

I am currently in the process of setting up Mozilla's BrowserQuest, but I have encountered a roadblock. The game relies on the now deprecated websocket-server node package, which has been removed from the npm library. In an attempt to find a solution ...

What is the best approach to ensure I wait for the next-auth session before utilizing useQuery()?

My NextJS project utilizes NextAuth for session management and React Query for data retrieval on the front-end. However, currently, the useSession() function returns undefined while checking if the session is authenticated. This undefined value is then us ...

Mapping an array based on specific conditions

When using my photo gallery app, I faced a challenge in mapping an array of images based on the user-selected album name. The current setup works perfectly for the 'Cambodia' album where all images are correctly logged in the console after mappin ...

Generate a .env configuration file using a .json document

When working on a project, .env files are essential for storing environment variables. I am exploring the possibility of programmatically modifying an .env file and it seems like using JSON (in a .json file) would be much simpler. Imagine having a file ca ...

forever js is interfering with the operation of a different application

I currently have two very similar Node JS projects that I manage by starting and stopping them using Forever JS. Both projects can run simultaneously on different ports but, when I input the following command: forever stop index.js In one project direc ...

Incorporating Swagger-UI into an Angular 10 project

Looking to integrate Swagger UI into your Angular application? After researching extensively, I found that the recommended approach is now to use the swagger-ui package instead of swagger-ui-dist for single-page applications. For those using React, there ...

Error: The function 'require(...)' is not a valid function when working with Sequelize in Node.js

I'm just starting out in web development and I'm attempting to build a Full Stack project using Mysql Express React Node.js. However, I've encountered a TypeError issue when using Sequelize with Node.js. Being new to this, I'm having tr ...

Executing NodeJS code without spawning a separate child process

In NodeJS, how can one achieve a c-level exec call similar to other languages and runtimes like Python's os.exec() or Bash's exec? Unlike using the child_process module (and all its existing wrappers), this method would fully replace the current ...

The specified module 'node:stream' lacks an export called 'getDefaultHighWaterMark'

I've encountered an issue while working on my Nuxt 3 project. After reinstalling Node using npm i, I'm getting the error message The requested module 'node:stream' does not provide an export named 'getDefaultHighWaterMark'. T ...

Transferring information from an iOS application to a Node.js server hosting a website

After creating my own Node.js version of a Sinatra webserver using Express, I successfully implemented code on the iPhone to retrieve data from the server by sending an HTTP request for '/sushi.json'. The server handles this request using app.get ...

Angular2 authguards encountering issues when trying to run asynchronous functions

I need a way to safeguard my routes by verifying if a user is logged in from the server, but I'm facing issues with asynchronous functions not executing properly. Below is the code snippet that's causing trouble: canActivate (route: ActivatedRo ...

Leveraging Ionic 2 with Moment JS for Enhanced TimeZones

I am currently working on integrating moment.js with typescript. I have executed the following commands: npm install moment-timezone --save npm install @types/moment @types/moment-timezone --save However, when I use the formattime function, it appears th ...

image encoding and preservation

As a novice in node.js, I am seeking advice on how to work with database selections. I have the required data (ID, building, floor, map) stored in a jpg format as a byte array 0x89504E470D0A1A0A0000000D4948... . I want to save this data along with a reduce ...