Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@darkce/react-native-webserver

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@darkce/react-native-webserver

A simple HTTP server for React Native apps

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-native-webserver

Install

npm install --save @darkce/react-native-webserver
With React Native 0.27+
react-native link @darkce/react-native-webserver

Example

First import react-native-webserver:

import { HttpServer } from "@darkce/react-native-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();
  }

Keywords

FAQs

Package last updated on 12 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc