I'm new to React Native and Node.js so this question may be quite obvious. I have React Native app running from "App.js".
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
export default class App extends React.Component
{
render()
{
return (
<View style={styles.container}>
<Button onPress={this.handlePress} title="Press me" />
</View>
);
}
handlePress()
{
fetch('http://10.222.22.22:3000/',{
method: 'POST',
body: JSON.stringify({
a: 'hello'
}),
headers: {"Content-Type": "application/json"}
})
.then(function(response){
return response.json()
}).catch(error => console.log(error));
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
(For security reasons, my IP isn't actually 10.222.22.22).
I also have a Node.js server running from "server.js".
var express = require('express');
var app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Here is where I'm trying to access and log the value of "a"
app.post('/', function (req, res) {
console.log(req.body.a);
});
app.listen(3000, function() {
console.log('Listening on port 3000');
});
I'm trying to access the value of "a" (from the fetch body in App.js handlePress()) after it is "sent" to the server. So far, nothing shows up on the console aside from "Listening on port 3000". Any help would be much appreciated, thanks!
handlePressis being called? At a first glance, nothing looks off here.