File tree Expand file tree Collapse file tree 3 files changed +58
-3
lines changed Expand file tree Collapse file tree 3 files changed +58
-3
lines changed Original file line number Diff line number Diff line change 3
3
"version" : " 0.1.0" ,
4
4
"private" : true ,
5
5
"devDependencies" : {
6
+ "npm-run-all" : " ^3.1.0" ,
6
7
"react-scripts" : " 0.6.1"
7
8
},
8
9
"dependencies" : {
10
+ "express" : " ^4.14.0" ,
9
11
"react" : " ^15.3.2" ,
10
12
"react-dom" : " ^15.3.2"
11
13
},
12
14
"scripts" : {
13
- "start" : " react-scripts start" ,
15
+ "start:dev " : " react-scripts start" ,
14
16
"build" : " react-scripts build" ,
15
17
"test" : " react-scripts test --env=jsdom" ,
16
- "eject" : " react-scripts eject"
17
- }
18
+ "eject" : " react-scripts eject" ,
19
+ "server" : " node server/server.js" ,
20
+ "dev" : " npm-run-all -p server start:dev" ,
21
+ "start" : " npm-run-all -s build server"
22
+ },
23
+ "proxy" : " http://localhost:3001"
18
24
}
Original file line number Diff line number Diff line change
1
+ const express = require ( 'express' ) ;
2
+ const app = express ( ) ;
3
+
4
+ // ポートの指定
5
+ app . set ( 'port' , process . env . PORT || 3001 ) ;
6
+
7
+ // html, js, cssの静的ファイル
8
+ var clientPath = __dirname . replace ( "/server" , "/build" ) ;
9
+ app . use ( '/' , express . static ( clientPath ) ) ;
10
+
11
+ // GET /todos でTODOを返す
12
+ app . get ( '/todos' , ( req , res , next ) => {
13
+ res . json ( [
14
+ { id : 0 , text : 'do something' } ,
15
+ { id : 1 , text : 'learn react' }
16
+ ] ) ;
17
+ } ) ;
18
+
19
+ // 指定したポートでリクエスト待機状態にする
20
+ app . listen ( app . get ( 'port' ) , ( ) => {
21
+ console . log ( 'server listening on port :' + app . get ( 'port' ) ) ;
22
+ } ) ;
Original file line number Diff line number Diff line change @@ -3,7 +3,26 @@ import logo from './logo.svg';
3
3
import './App.css' ;
4
4
5
5
class App extends Component {
6
+
7
+ constructor ( props ) {
8
+ super ( props ) ;
9
+ this . state = {
10
+ todos : [ ]
11
+ } ;
12
+ }
13
+
14
+ componentDidMount ( ) {
15
+ fetch ( '/todos' )
16
+ . then ( response => {
17
+ console . log ( response ) ;
18
+ return response . json ( ) ;
19
+ } )
20
+ . then ( todos => this . setState ( { todos : todos } ) )
21
+ . catch ( e => console . log ( e ) ) ;
22
+ }
23
+
6
24
render ( ) {
25
+ console . log ( 'todos' , this . state . todos ) ;
7
26
return (
8
27
< div className = "App" >
9
28
< div className = "App-header" >
@@ -13,6 +32,14 @@ class App extends Component {
13
32
< p className = "App-intro" >
14
33
To get started, edit < code > src/App.js</ code > and save to reload.
15
34
</ p >
35
+ < div >
36
+ < h1 > todos</ h1 >
37
+ < ol >
38
+ { this . state . todos . map ( todo => (
39
+ < li key = { todo . id } > { todo . text } </ li >
40
+ ) ) }
41
+ </ ol >
42
+ </ div >
16
43
</ div >
17
44
) ;
18
45
}
You can’t perform that action at this time.
0 commit comments