A cross platform component for serving static assets with React Native.
$ npm install react-native-httpd --save
Mostly automatic installation
$ react-native link react-native-httpd
- In XCode, in the project navigator, right click
Add Files to [your project's name]
- Go to
- In XCode, in the project navigator, select your project. Add
libFPStaticServer.ato your project's
Link Binary With Libraries
- Run your project (
- Open up
import com.futurepress.staticserver.FPStaticServerPackage;to the imports at the top of the file
new FPStaticServerPackage()to the list returned by the
- Append the following lines to
include ':react-native-httpd' project(':react-native-httpd').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-httpd/android')
- Insert the following lines inside the dependencies block in
StaticServer with a port or use the default
0 to pick a random available port.
;let server = 8080;// Start the serverserverstart;// Stop the serverserver;
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)
;;// create a path you want to write tolet path = RNFSDocumentDirectoryPath + '/www';let server = 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.
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.
;;// path where files will be served from (index.html here)let path = RNFSMainBundlePath + '/www';let server = 8080 path;
If the server should only be accessible from within the app, set
;// Just set options with defaultslet server = localOnly : true ;// Or also valid are:let server = 8080 localOnly : true ;let server = 8080 path localOnly : true ;
If the server should not pause when the app is in the background, set
let server = keepAlive : true ;
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
let server = 0 keepAlive : true ;