geckosio / phaser-on-nodejs

Allows you to run Phaser 3 game (including Phaser's physics engines) on Node.js
MIT License
102 stars 11 forks source link

HTTP-Request gets blocked via FakeXMLHttpRequest #11

Closed CodingDick closed 2 years ago

CodingDick commented 2 years ago

Describe the bug I would like to send a http-request, but all request get handled by the FakeXMLHttpRequest class. The reason is, that this particular class overwrites HTTP-Request globally. https://github.com/geckosio/phaser-on-nodejs/blob/master/src/index.ts#L21

My only chance to send an normal request is to comment those line out, because i can not see a config for that.

Screenshot 2021-12-22 at 15 12 04

Have a question? Why do you need that "faker" and could it be possible to configure it?

yandeu commented 2 years ago

The fakeXMLHttpRequest is needed since Node.js does not provide this function.

This library works on Node.js where fakeXMLHttpRequest does not exist so it would not even be possible to make a http request using fakeXMLHttpRequest. To make http requests on Node.js, I recommend you using node-fetch.

CodingDick commented 2 years ago

I am using the Morlalis SDK-Package for node-js. And overwriting this globally prevends other packages to use axios for example: https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/7ac097aa79e6faefcae4b15b62b5005e0a98430e/src/MoralisWeb3Api.js#L127

yandeu commented 2 years ago

Oh yes. Axios is isomorphic and therefore uses XMLHttpRequest.

CodingDick commented 2 years ago

@yandeu, sorry but i do not get what you want to tell me (:

yandeu commented 2 years ago

I just added a description. Please try it: https://github.com/geckosio/phaser-on-nodejs#using-node-fetch-or-axios

CodingDick commented 2 years ago

Thanks for that edit, but it does not help me. The package i am using, uses axios. And i can not modify this package and control the initialization of axios.

Moralis uses axios under the hood:


    let tokenInfo = await Moralis.Web3API.account.getNFTsForContract({ chain: 'matic', token_address: TOKEN_ADRESS, address: user.ethAddress })
yandeu commented 2 years ago

Try the hack around tokenInfo or when you call something from Moralis.

Unfortunately there is no other workaround. Unfortunately, axios' "isBrowserCheck" is not very reliable. Maybe you could make a PR for axios.

Or remove axios from Moralis.

CodingDick commented 2 years ago

Sorry for replying that late. But i got a great solution for that. I got rid of Moralis and used web3.js directly instead to gather information from the polygon blockchain. For anyone having the same issue, try using web3.js

Your mentionted trick works because web3.js does this under the hood: ` HttpProvider.prototype._prepareRequest = function () {

var request;
// the current runtime is a browser
if (typeof XMLHttpRequest !== 'undefined') {
    request = new XMLHttpRequest();
}

`