Guest User

Untitled

a guest
Nov 28th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import './App.css';
  3.  
  4. class App extends Component {
  5. constructor(props) {
  6.     super();
  7.     this.state = {
  8.       title: 'Simple postgres app',
  9.       treatments: []
  10.     }
  11. }
  12.  
  13. componentDidMount(){
  14.     console.log('COMPONENT HAS MOUNTED')
  15. }
  16.  
  17. addStuff(event){
  18. event.preventDefault()
  19. // console.log('in method');
  20.  
  21. let data = {
  22. test_field: this.refs.test_field.value,
  23. };
  24.  
  25. var request = new Request('http://localhost:3000/api/new-thing', {
  26. method: 'POST',
  27. headers: new Headers({ 'Content-Type': 'application/json' }),
  28. body: JSON.stringify(data),
  29. message: console.log('JSON output: ', JSON.stringify(data))
  30. });
  31.  
  32. fetch(request)
  33. .then((response) => {
  34. response.json()
  35. .then((data) => {
  36. console.log(data)
  37. })
  38. })
  39. }
  40.  
  41. render() {
  42. let title = this.state.title;
  43. return (
  44. <div className="App">
  45. <h1> { title } </h1>
  46. <form ref = "testForm">
  47. <input type="text" ref="test_field" placeholder="test_field"/>
  48. <button onClick={this.addStuff.bind(this)}>Add This</button>
  49. </form>
  50. </div>
  51. );
  52. }
  53. }
  54.  
  55. export default App;
  56.  
  57. let express = require('express');
  58. let bodyParser = require('body-parser');
  59. let morgan = require('morgan');
  60. let pg = require('pg');
  61. const PORT = 3000;
  62.  
  63. // let pool = new pg.Pool({
  64. //     port: 5432,
  65. //     user: 'postgres',
  66. //     password: 'postgres',
  67. //     database: 'po1dev_v0.0.1',
  68. //     max: 10, //max connections
  69. //     host: 'localhost'
  70. // })
  71.  
  72. let app = express();
  73.  
  74. app.use(bodyParser.json);
  75. app.use(bodyParser.urlencoded({ extended:true }));
  76.  
  77. app.use(morgan('dev'));
  78.  
  79. app.use((request, response, next) => {
  80.     response.header("Access-Control-Allow-Origin", "*");
  81.     response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  82.     next();
  83. });
  84.  
  85. // app.post('/api/new-thing', (request,response) => {
  86. // console.log(request.body)
  87. // })
  88. app.post('/api/new-thing', function(request,response){
  89. console.log(request.body);
  90. })
  91.  
  92. app.listen(PORT, () => console.log('Listening on port ' + PORT));
Add Comment
Please, Sign In to add comment