Following this quick guide (React and PostgreSQL), the following app should print the JSON fetch to the bash terminal (at ~37min of video).
However this does not happen. There is no feedback on the npm or nodemon servers.
When adding a value via the front-end, firefox instantly sends back a 404 status (observed in console:network). In chrome, the thread hangs as pending until the nodemon server is shut down (and then fails with a connection reset error)(again in console:network).
npm is running app and nodemon is running the server.
app.js
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super();
this.state = {
title: 'Simple postgres app',
treatments: []
}
}
componentDidMount(){
console.log('COMPONENT HAS MOUNTED')
}
addStuff(event){
event.preventDefault()
// console.log('in method');
let data = {
test_field: this.refs.test_field.value,
};
var request = new Request('http://localhost:3000/api/new-thing', {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify(data),
message: console.log('JSON output: ', JSON.stringify(data))
});
fetch(request)
.then((response) => {
response.json()
.then((data) => {
console.log(data)
})
})
}
render() {
let title = this.state.title;
return (
<div className="App">
<h1> { title } </h1>
<form ref = "testForm">
<input type="text" ref="test_field" placeholder="test_field"/>
<button onClick={this.addStuff.bind(this)}>Add This</button>
</form>
</div>
);
}
}
export default App;
server.js
let express = require('express');
let bodyParser = require('body-parser');
let morgan = require('morgan');
let pg = require('pg');
const PORT = 3000;
// let pool = new pg.Pool({
// port: 5432,
// user: 'postgres',
// password: 'postgres',
// database: 'po1dev_v0.0.1',
// max: 10, //max connections
// host: 'localhost'
// })
let app = express();
app.use(bodyParser.json);
app.use(bodyParser.urlencoded({ extended:true }));
app.use(morgan('dev'));
app.use((request, response, next) => {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// app.post('/api/new-thing', (request,response) => {
// console.log(request.body)
// })
app.post('/api/new-thing', function(request,response){
console.log(request.body);
})
app.listen(PORT, () => console.log('Listening on port ' + PORT));
Any ideas on what may be causing the 404/hang problems in firefox/chrome and how to go about fixing it?
Cheers
That's because the route you're creating doen't return any response so it waits indefinitely for a response then gets timed out.
The route should return some response,
app.post('/api/new-thing', function(request,response){
console.log(request.body);
return response.json({"data": "Hello World"})
})
Which will return the {"data": "Hello World"}
from the /api/new-thing
route.
Also, bodyParser.json
is a function not property. Change it to
app.use(bodyParser.json())
If you are using create-react-app
try another port for the backend server. Because by default it uses 3000 for the react app.