alwx / react-native-http-bridge

HTTP server for React Native
118 stars 78 forks source link

Can't get the readme example to work #23

Open madhums opened 4 years ago

madhums commented 4 years ago

Hi, I've tried to get this library to work but it's really not my cup of tea. I've spent a day and I am not getting anywhere. I'd like to share some progress I made, hope it helps others or the author himself to resolve issues.

To get this library partially working, I did the following:

  1. PR #22
  2. npx react-native link react-native-http-bridge
    cd ios && pod install && cd ..
    expo start -c

P.S: I have ejected expo 36.0.0 (which I think uses RN 0.61.4)

Example code:

import HttpBridge from 'react-native-http-bridge'

HttpBridge.start(5555, 'http_service', function(req) {
  console.log(req); // this function gets called 3 times for some reason
  HttpBridge.respond(
    req.requestId,
    200,
    'application/json',
    '{"message": "OK"}' // not able to receive this response
  );
});

fetch('http://127.0.0.1:5555/users')
  .then(console.log) // no response here

iOS Build failures: There are two locations where xcode build fails

  1. start method. throws "// throws Use of undeclared identifier '_requestResponses'"

    - _requestResponses = [[NSMutableDictionary alloc] init]; 
    + NSMutableDictionary *_requestResponses = [[NSMutableDictionary alloc] init];
  2. respond method. throws "Thread 4: EXC_BAD_ACCESS (code=1, address=0x10)"

    - completionBlock(requestResponse);
    + if (completionBlock) completionBlock(requestResponse);

These changes seem to succeed the build but I am not sure if they have any side affects.

Logs:

Object {
  "requestId": "1579266250521:175873",
  "type": "GET",
  "url": "/users",
}
Object {
  "requestId": "1579266250524:571040",
  "type": "GET",
  "url": "/users",
}
Object {
  "requestId": "1579266250526:557842",
  "type": "GET",
  "url": "/users",
}

Network request failed
- node_modules/whatwg-fetch/dist/fetch.umd.js:473:29 in xhr.onerror
- node_modules/event-target-shim/dist/event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:574:29 in setReadyState
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:190:12 in emit
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:436:47 in __callFunction
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:111:26 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:384:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

On Android I was unable to get the "react-native link" work, so it's unable to recognise start() method and throws

[Unhandled promise rejection: TypeError: null is not an object (evaluating 'Server.start')]
- node_modules/react-native-http-bridge/httpServer.js:13:12 in module.exports.start
- node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
- node_modules/regenerator-runtime/runtime.js:271:30 in invoke
- node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
Alwinator commented 1 year ago

@madhums Feel free to checkout: react-native-http-bridge-refurbished