{"library":"react-native-static-server","title":"React Native Static HTTP Server","description":"react-native-static-server provides a cross-platform HTTP static file server component for React Native applications, enabling them to serve local assets over HTTP. The current stable version is 0.5.0, released in March 2024, indicating a moderate release cadence. It leverages native web server implementations (GCDWebServer for iOS and NanoHttpd for Android) to offer robust performance. Key differentiators include its seamless integration with `react-native-fs` for managing file paths, options for serving from the document directory or the app's main bundle, and configurations for local-only access and maintaining server activity while the app is in the background via a `keepAlive` option. It's designed to simplify serving packaged web content or dynamically created local files within a React Native environment.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install react-native-static-server"],"cli":null},"imports":["import StaticServer from 'react-native-static-server';","import RNFS from 'react-native-fs';","new StaticServer({ localOnly: true, keepAlive: true });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import StaticServer from 'react-native-static-server';\nimport RNFS from 'react-native-fs';\nimport { Platform } from 'react-native';\n\nconst startStaticServer = async () => {\n  const serverPort = 8080;\n  \n  // Create a directory for static files if it doesn't exist\n  const wwwPath = `${RNFS.DocumentDirectoryPath}/www`;\n  if (!(await RNFS.exists(wwwPath))) {\n    await RNFS.mkdir(wwwPath);\n  }\n\n  // Write a simple index.html file\n  const indexPath = `${wwwPath}/index.html`;\n  await RNFS.writeFile(indexPath, '<h1>Hello from Static Server!</h1><p>Served from: ' + wwwPath + '</p>', 'utf8');\n  console.log(`Wrote index.html to ${indexPath}`);\n\n  // Instantiate the server\n  const server = new StaticServer(serverPort, wwwPath, { \n    localOnly: true,\n    keepAlive: true \n  });\n\n  try {\n    const url = await server.start();\n    console.log('Static server started at URL:', url);\n    console.log('Access it on your device at:', `${Platform.OS === 'ios' ? 'http://localhost' : 'http://127.0.0.1'}:${serverPort}/index.html`);\n    // In a real app, you might store the server instance or its URL\n    // to stop it later or open a WebView.\n  } catch (error) {\n    console.error('Failed to start static server:', error);\n  }\n\n  // To stop the server later:\n  // server.stop().then(() => console.log('Server stopped'));\n};\n\nstartStaticServer();","lang":"typescript","description":"Demonstrates how to initialize and start a static HTTP server, write a simple HTML file to the document directory, and serve it. It includes options for local-only access and keeping the server alive in the background. Note: `react-native-fs` is required for this example.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}