Advertisement
Guest User

Untitled

a guest
Aug 27th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #########################################################
  2. // index.js < server
  3.  
  4. const express = require('express')
  5. const app = express()
  6.  
  7. var cors = require('cors')
  8. var bodyParser = require('body-parser')
  9.  
  10. app.use(cors())
  11. app.use(bodyParser.json())
  12.  
  13. const LOGIN = 'uzivatel';
  14. const PASS = 'heslicko';
  15. const users = [{
  16. id: '1',
  17. name: 'uzivatel'
  18. }];
  19.  
  20. /*
  21. app.get('*', (req, res) => res.send(`
  22. <html>
  23. <body>
  24. 123
  25. </body>
  26. </html>
  27. `))
  28. */
  29.  
  30. app.get('/' , (req, res) => res.json({
  31. message: 'Hello worldi! z jsonu'
  32. }))
  33. app.get('/api/users', (req, res) => {
  34. res.json(users);
  35. })
  36. app.post('/api/add-user', (req,res) => {
  37. const { name } = req.body;
  38.  
  39. users.push({
  40. id: users.length + 1,
  41. name
  42. });
  43. res.json(users)
  44. console.info(users)
  45. });
  46.  
  47. app.post('/api/check-login', (req, res) => {
  48. const { username, password} = req.body;
  49. res.json({
  50. valid:username === LOGIN && password === PASS
  51. })
  52. })
  53. app.listen(3001, () => console.log('Example app listening on port 3001'))
  54.  
  55. ################################################
  56. //Client app.js
  57.  
  58. import React, { Component } from 'react';
  59. import './App.css';
  60.  
  61. class App extends Component {
  62. state = {
  63. users:[],
  64. name:'',
  65. isAdmin: false,
  66. }
  67.  
  68. async componentDidMount(){
  69. const response = await fetch('http://localhost:3001/api/users')
  70. const users = await response.json();
  71. this.setState({
  72. users
  73. });
  74. }
  75.  
  76. addUser = async () => {
  77. const response = await fetch('http://localhost:3001/api/add-user', {
  78. method: 'POST',
  79. headers: {
  80. "Content-Type": "application/json; charset=utf-8",
  81. },
  82. body: JSON.stringify({
  83. name: this.state.name
  84. })
  85. })
  86. const users = await response.json();
  87. console.info(users)
  88. this.setState({
  89. users
  90. });
  91. }
  92. changeName = (e) => {
  93. this.setState({
  94. name: e.target.value
  95. })
  96. }
  97.  
  98. checkCredentials = async () => {
  99. const response = await fetch('http://localhost:3001/api/check-login', {
  100. method: 'POST',
  101. headers: {
  102. "Content-Type": "application/json; charset=utf-8",
  103. },
  104. body: JSON.stringify({
  105. username: 'uzivatel',
  106. password: 'heslicko',
  107. })
  108. });
  109. const data = await response.json();
  110. this.setState({
  111. isAdmin:data.valid
  112. })
  113. }
  114. render() {
  115. const { users, name, isAdmin } = this.state;
  116. return (
  117. <div className="App">
  118. {users.map((user) => <div key={user.id}> {user.name}</div>)}
  119. <input type="text" value={name} onChange={this.changeName}/>
  120. <button onClick={this.addUser}>Pridej uživatele</button> <br/>
  121. <button onClick={this.checkCredentials}>Check Credentials</button>
  122. {isAdmin ? 'Je to admin' : ''}
  123. </div>
  124. );
  125. }
  126. }
  127.  
  128. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement