Skip to content
master
Switch branches/tags
Code
This branch is 9 commits ahead, 56 commits behind futurepress:master.

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
ios
 
 
 
 
 
 
 
 
 
 
 
 
 
 

react-native-static-server

A cross platform component for serving static assets with React Native.

Getting started

$ npm install react-native-static-server --save

Mostly automatic installation

$ react-native link react-native-static-server

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-static-server and add FPStaticServer.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libFPStaticServer.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import com.futurepress.staticserver.FPStaticServerPackage; to the imports at the top of the file
  • Add new FPStaticServerPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-static-server'
    project(':react-native-static-server').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-static-server/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-static-server')
    

Windows (UWP only)

  1. In Visual Studio, in the solution explorer, right click Solution [your project's name]AddExisting project...
  2. Go to node_modulesreact-native-static-server and add FPStaticServer.UWP.csproj
  3. In your freshly imported FPStaticServer.UWP project, right click ReferencesAdd reference...
  4. In the left menu, select Projects > Solution and tick ReactNative. Press OK to confirm
  5. In your main project, right click ReferencesAdd reference...
  6. In the left menu, select Projects > Solution and tick FPStaticServer.UWP. Press OK to confirm
  7. In your main project, open MainPage.cs (or whatever ReactPage you load) and modify your Packages override:
    using FPStaticServer;
    // ... inside MainPage class
    public override List<IReactPackage> Packages
    {
    	get
    	{
    		return new List<IReactPackage>
    		{
    			new MainReactPackage(),
    			new FPStaticServerReactPackage(),
    			// other packages
    		};
    	}
    }

Usage

Declare the StaticServer with a port or use the default 0 to pick a random available port.

import StaticServer from 'react-native-static-server';

let server = new StaticServer(8080);

// Start the server
server.start().then((url) => {
  console.log("Serving at URL", url);
});

// Stop the server
server.stop();

StaticServer serves from the document directory (default) or takes an optional absolute path to serve from.

For instance, using react-native-fs you can get the document directory and specify a directory from there.

Default (document directory)

import StaticServer from 'react-native-static-server';
import RNFS from 'react-native-fs';

// create a path you want to write to
let path = RNFS.DocumentDirectoryPath + '/www';

let server = new StaticServer(8080, path);

Custom folder (iOS)

Create the folder for static files

Create a folder in your project's top-level directory (usually next to your node_modules and index.js file), and put the files you want to access over http in there.

Add folder (static files) to XCode

This folder must be added to XCode so it gets bundled with the app.

In XCode, Project Navigator right click in the folder project → Add files to "<project>" → Select the static folder and clic options (Uncheck copy items if needed, Create folder references) so don't duplicate files → Clic Add.

When the app gets bundled, this folder will be next to the compiled app, so using MainBundlePath property from react-native-fs you can access to the directory.

import StaticServer from 'react-native-static-server';
import RNFS from 'react-native-fs';

// path where files will be served from (index.html here)
let path = RNFS.MainBundlePath + '/www';

let server = new StaticServer(8080, path);

If the server should only be accessible from within the app, set localOnly to true

import StaticServer from 'react-native-static-server';

// Just set options with defaults
let server = new StaticServer({localOnly : true });
// Or also valid are:
let server = new StaticServer(8080, {localOnly : true });
let server = new StaticServer(8080, path, {localOnly : true });

If the server should not pause when the app is in the background, set keepAlive to true

let server = new StaticServer({keepAlive : true });

Passing 0 as the port number will cause a random port to be assigned every time the server starts. It will reset to a new random port each time the server unpauses, so this should only be used with keepAlive.

let server = new StaticServer(0, {keepAlive : true });

Credits

Thanks to CorHttpd and react-native-httpserver for the basis of this library.

About

HTTP static file server for React Native

Resources

License

Releases

No releases published

Packages

No packages published

Languages