birdofpreyru / react-native-static-server

Embedded HTTP server for React Native
https://dr.pogodin.studio/docs/react-native-static-server
Other
142 stars 22 forks source link

403 Forbidden #34

Closed frederiko-famobi closed 1 year ago

frederiko-famobi commented 1 year ago

Hello, after following the example project, I managed to make the server work.

But now I am getting an error 403 Forbiddenon the webview for Android.

image

My project logs (following the example project) are the following:

LOG Extracting web server assets... LOG Server #20844. New state: "STARTING". Details: "". LOG Server #20844. New state: "ACTIVE". Details: "".

I checked the origin state and the uri is http://localhost:34797.

I tried to add to the webview:

      originWhitelist={['*']}
          source={{
            uri: origin,
            headers: {
              'Access-Control-Allow-Origin': '*',
            },
          }}

I also changed the assets files/folder to chmod 777.

But no success.

I am currently using Expo and had to install the library react-native-fs so the project would work. Usually we use expo-file-system, not sure if this is related or not.

If I set the property in the server to nonLocal: true and try to open in the browser and the same error 403 appears.

How can I solve this problem?

birdofpreyru commented 1 year ago

Well, you remember, Expo isn't officially supported (#8, #19). I bet, with the regular Android build example app works fine with last release (I just double-checked it): image

Anyway, what you shared so far sounds like the server per se works fine. Are you actually running the example app as is? Because, my first guess would be that you accessing http://localhost:34797 without having any index file in the folder served by the server (which is configured to be one of these, and in the example app it would be index.html), so it probably attempts to list all content of the served folder, it is denied by OS, and thus it responds with 403 error. Though, it presumably able to see the directory to be served exists, as otherwise it would respond with 404 or 500 error.

frederiko-famobi commented 1 year ago

Hello @birdofpreyru, after your reply and analysing the example code, I noticed that in the Expo generated android files, it was missing the following config in android/app/build.gradle:

android {
 ... 

    sourceSets {
      main {
        assets.srcDirs = ['../../assets']
      }
    }

After adding this, it worked fine. :) Now I see the same as you sent in your screenshot. Thank you!!