expo-http-server 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,101 @@
|
|
|
1
|
+
|
|
1
2
|
# expo-http-server
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
[](https://www.npmjs.com/package/expo-http-server)
|
|
5
|
+
[](https://www.npmjs.com/package/expo-http-server)
|
|
6
|
+
[](https://github.com/simonsturge/expo-http-server)
|
|
7
|
+
[](https://github.com/simonsturge/expo-http-server)
|
|
8
|
+
|
|
9
|
+
A simple HTTP Server [Expo module](https://docs.expo.dev/modules/) .
|
|
10
|
+
|
|
11
|
+
Current implementation is for iOS / Android only ([React Native](https://github.com/facebook/react-native)).
|
|
12
|
+
|
|
13
|
+
iOS: [Criollo](https://github.com/thecatalinstan/Criollo)
|
|
14
|
+
Android: [AndroidServer](https://github.com/fengzhizi715/AndroidServer)
|
|
15
|
+
Web: **Not implemented**
|
|
16
|
+
|
|
17
|
+
This is a work in progress and not yet used in a production app (there are surely things I've overlooked), but please do make a PR for any improvements / problems you can see!
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
npx expo install expo-http-server
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Example
|
|
26
|
+
```tsx
|
|
27
|
+
import * as server from "expo-http-server";
|
|
28
|
+
import { useEffect, useState } from "react";
|
|
29
|
+
import { Text, View } from "react-native";
|
|
30
|
+
|
|
31
|
+
export default function App() {
|
|
32
|
+
const [lastCalled, setLastCalled] = useState<number | undefined>();
|
|
33
|
+
|
|
34
|
+
const html = `
|
|
35
|
+
<!DOCTYPE html>
|
|
36
|
+
<html>
|
|
37
|
+
<body style="background-color:powderblue;">
|
|
38
|
+
<h1>expo-http-server</h1>
|
|
39
|
+
<p>You can load HTML!</p>
|
|
40
|
+
</body>
|
|
41
|
+
</html>`;
|
|
42
|
+
|
|
43
|
+
const obj = { app: "expo-http-server", desc: "You can load JSON!" };
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
server.setup(9666);
|
|
47
|
+
server.route("/", "GET", async (request) => {
|
|
48
|
+
console.log("Request", "/", "GET", JSON.stringify(request));
|
|
49
|
+
setLastCalled(Date.now());
|
|
50
|
+
return {
|
|
51
|
+
statusCode: 200,
|
|
52
|
+
contentType: "application/json",
|
|
53
|
+
body: JSON.stringify(obj),
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
server.route("/html", "GET", async (request) => {
|
|
57
|
+
console.log("Request", "/html", "GET", JSON.stringify(request));
|
|
58
|
+
setLastCalled(Date.now());
|
|
59
|
+
return {
|
|
60
|
+
statusCode: 200,
|
|
61
|
+
contentType: "text/html",
|
|
62
|
+
body: html,
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
server.start();
|
|
66
|
+
return () => {
|
|
67
|
+
server.stop();
|
|
68
|
+
};
|
|
69
|
+
}, []);
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<View
|
|
73
|
+
style={{
|
|
74
|
+
flex: 1,
|
|
75
|
+
backgroundColor: "#fff",
|
|
76
|
+
alignItems: "center",
|
|
77
|
+
justifyContent: "center",
|
|
78
|
+
}}
|
|
79
|
+
>
|
|
80
|
+
<Text>
|
|
81
|
+
{lastCalled === undefined
|
|
82
|
+
? "Request webserver to change text"
|
|
83
|
+
: "Called at " + new Date(lastCalled).toLocaleString()}
|
|
84
|
+
</Text>
|
|
85
|
+
</View>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Not that on iOS when the app is backgrounded the server will inevitably get paused. A background task will be started automatically, however you will likely only get 30 seconds of listening time in the background.
|
|
92
|
+
|
|
93
|
+
Send a request to the server in a browser `browser` or `curl`:
|
|
4
94
|
|
|
5
|
-
|
|
95
|
+
```shell
|
|
96
|
+
curl http://IP_OF_DEVICE:MY_PORT
|
|
97
|
+
```
|
|
98
|
+
For example:
|
|
99
|
+
```shell
|
|
100
|
+
curl http://192.168.1.109:3000
|
|
101
|
+
```
|