Skip to content

Commit 125bf50

Browse files
committed
iOS start fixes
1 parent 757b2a7 commit 125bf50

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { NativeModules } from 'react-native';
44
const { FPStaticServer } = NativeModules;
55

66
const PORT = "9999";
7-
const ROOT = "";
7+
const ROOT = null;
88
const LOCALHOST = 'http://127.0.0.1:';
99

1010
class StaticServer {
@@ -50,7 +50,7 @@ class StaticServer {
5050
if( this.started ){
5151
console.warn('StaticServer already running');
5252
}
53-
this.port = this.port;
53+
5454
this.origin = LOCALHOST + this.port;
5555

5656
this.started = true;
@@ -68,4 +68,5 @@ class StaticServer {
6868
return FPStaticServer.stop();
6969
}
7070
}
71+
7172
export default StaticServer;

ios/FPStaticServer.m

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ - (NSString *)getIPAddress:(BOOL)preferIPv4
4444
NSArray *searchArray = preferIPv4 ?
4545
@[ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
4646
@[ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;
47-
47+
4848
NSDictionary *addresses = [self getIPAddresses];
4949
NSLog(@"addresses: %@", addresses);
50-
50+
5151
__block NSString *address;
5252
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
5353
{
@@ -60,7 +60,7 @@ - (NSString *)getIPAddress:(BOOL)preferIPv4
6060
- (NSDictionary *)getIPAddresses
6161
{
6262
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
63-
63+
6464
// retrieve the current interfaces - returns 0 on success
6565
struct ifaddrs *interfaces;
6666
if(!getifaddrs(&interfaces)) {
@@ -97,46 +97,44 @@ - (NSDictionary *)getIPAddresses
9797
return [addresses count] ? addresses : nil;
9898
}
9999

100-
RCT_EXPORT_METHOD(port:(NSString *)port
100+
RCT_EXPORT_METHOD(start: (NSString *)port
101101
root:(NSString *)optroot
102-
localOnly:(NSString *)localhost_only
102+
localOnly:(BOOL *)localhost_only
103103
resolver:(RCTPromiseResolveBlock)resolve
104104
rejecter:(RCTPromiseRejectBlock)reject) {
105105

106106
NSString * root;
107-
107+
108108
if( [optroot length] == 0 ){
109109
root = [NSString stringWithFormat:@"%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] ];
110110
} else if([optroot hasPrefix:@"/"]) {
111111
root = optroot;
112112
} else if( [optroot isEqualToString:@"BUNDLE"] ){
113-
root = [NSString stringWithFormat:@"%@/www", [[NSBundle mainBundle] bundlePath] ];
114-
} else if( [optroot isEqualToString:@"DOCS"] ){
115-
root = [NSString stringWithFormat:@"%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] ];
113+
root = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] bundlePath] ];
116114
}
117-
118-
115+
116+
119117
if(root) self.www_root = root;
120-
118+
121119
if(port) self.port = [port intValue];
122-
123-
if(localhost_only) self.localhost_only = [localhost_only boolValue];
124-
120+
121+
self.localhost_only = localhost_only;
122+
125123
if(self.httpServer != nil) {
126124
if([self.httpServer isRunning]) {
127125
NSError *error = nil;
128126
reject(@"server_error", @"server is already up", error);
129127
return;
130128
}
131129
}
132-
130+
133131
[DDLog addLogger:[DDTTYLogger sharedInstance]];
134132
self.httpServer = [[HTTPServer alloc] init];
135-
133+
136134
[self.httpServer setPort:self.port];
137-
135+
138136
if(self.localhost_only) [self.httpServer setInterface:IP_LOCALHOST];
139-
137+
140138
// Serve files from our embedded Web folder
141139
const char * docroot = [self.www_root UTF8String];
142140
if(*docroot == '/') {
@@ -147,40 +145,39 @@ - (NSDictionary *)getIPAddresses
147145
}
148146
NSLog(@"Setting document root: %@", self.localPath);
149147
[self.httpServer setDocumentRoot:self.localPath];
150-
151-
148+
149+
152150
NSError *error;
153151
if([self.httpServer start:&error]) {
154152
int listenPort = [self.httpServer listeningPort];
155153
NSString* ip = self.localhost_only ? IP_LOCALHOST : [self getIPAddress:YES];
156154
NSLog(@"Started httpd on port %d", listenPort);
157-
155+
158156
self.url = [NSString stringWithFormat:@"http://%@:%d/", ip, listenPort];
159157
resolve(self.url);
160-
158+
161159
} else {
162160
NSLog(@"Error starting httpd: %@", error);
163-
161+
164162
// NSString* errmsg = [error description];
165163
reject(@"server_error", @"server could not start", error);
166-
164+
167165
}
168-
166+
169167
}
170168

171169
RCT_EXPORT_METHOD(stop) {
172170
if(self.httpServer != nil) {
173-
171+
174172
[self.httpServer stop];
175173
self.httpServer = nil;
176-
174+
177175
self.localPath = @"";
178176
self.url = @"";
179-
177+
180178
NSLog(@"httpd stopped");
181179
}
182180
}
183181

184182

185183
@end
186-

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{
33
"name": "react-native-static-server",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"repository": "https://github.com/futurepress/react-native-static-server",
66
"description": "HTTP static file server for React Native",
77
"main": "index.js",

0 commit comments

Comments
 (0)