Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- 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));
Add Comment
Please, Sign In to add comment