@react-native-library/webserver
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

react-native-webserver

Install

npm install --save @react-native-library/webserver

Automatically link

With React Native 0.27+

react-native link @react-native-library/webserver

Example

First import @react-native-library/webserver:

import { HttpServer } from "@react-native-library/webserver";

Initalize the server in the componentWillMount lifecycle method. You need to provide a port and a callback.

    componentWillMount() {
      // initalize the server (now accessible via localhost:1234)
      HttpServer.start(5561, 'http_service' (request, response) => {

          // you can use request.url, request.type and request.postData here
          if (request.method === "GET" && request.url.startsWith('/users')) {
            response.send(200, "application/json", "{\"message\": \"OK\"}");
          } else if (request.method === "GET" && request.url.startsWith('/image.jpg')) {
            response.sendFile('xxx/xxx.jpg');
          } else {
            response.send(400, "application/json", "{\"message\": \"Bad Request\"}");
          }

      });
    }

Finally, ensure that you disable the server when your component is being unmounted.

  componentWillUnmount() {
    HttpServer.stop();
  }

Package Sidebar

Install

npm i @react-native-library/webserver

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

81 kB

Total Files

20

Last publish

Collaborators

  • luoxuhai