0.0.3 • Published 5 years ago
http-server-react-native v0.0.3
react-native-http-server
An HTTP server for react native based on https://github.com/swisspol/GCDWebServer
Getting started
$ npm install http-server-react-native --save
Mostly automatic installation
$ react-native link http-server-react-native
Manual installation
iOS
- In XCode, in the project navigator, right click
Libraries➜Add Files to [your project's name] - Go to
node_modules➜react-native-http-serverand addRNHttpServer.xcodeproj - In XCode, in the project navigator, select your project. Add
libRNHttpServer.ato your project'sBuild Phases➜Link Binary With Libraries - Run your project (
Cmd+R)<
Android
Not supported yet
Usage
import React, { Component } from 'react';
import { HttpServer, Router } from 'http-server-react-native';
class Server extends Component {
componentDidMount() {
const router = new Router('/path');
router
.get(data => {
return { status: 200, data: { some: 'data' } };
})
.post(data => {
return { status: 200, data: { some: 'data'} };
})
.put(data => {
return { status: 200, data: { some: 'data'} };
})
.patch(data => {
return { status: 200, data: { some: 'data'} };
})
.delete(data => {
return { status: 200, data: { some: 'data'} };
})
this.server = new HTTPServer({ port: 8080 });
this.server.registerRouter(router);
this.server
.start()
.then(url => console.log(`Server running on ${url}`)
.catch(err => console.error(err));
}
componentWillUnmount() {
this.server.stop()
}
...
}
;