全局安装 express-generatorcss
$ npm install -g express-generator
建立express appnode
$ express react-backend
安装依赖包react
$ npm install
修改react-backend/routes/users.js, 返回简单的数据,方便测试express
var express = require('express'); var router = express.Router(); /* GET users listing. */ router.get('/', function(req, res, next) { // Comment out this line: //res.send('respond with a resource'); // And insert something like this instead: res.json([{ id: 1, username: "samsepi0l" }, { id: 2, username: "D0loresH4ze" }]); }); module.exports = router;
启动express appnpm
$ PORT=3001 node bin/www
把端口设置成3001的缘由是由于react app 会使用3000端口,避免冲突json
全局安装 create-react-appvim
$ npm install -g create-react-app
在react-backend文件夹下建立react app(也能够在其余文件夹下)浏览器
# 在这里安装的时候 真心很慢,回头我研究一下,改为本身建立react应用,应该会快一点 $ create-react-app client
设置proxy服务器
$ cd client $ vim package.json # 代码 "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" }, "proxy": "http://localhost:3001"
proxy 是你后台服务器的地址app
修改client/src/App.js
import React, { Component } from 'react'; import './App.css'; class App extends Component { state = {users: []} componentDidMount() { fetch('/users') .then(res => res.json()) .then(users => this.setState({ users })); } render() { return ( <div className="App"> <h1>Users</h1> {this.state.users.map(user => <div key={user.id}>{user.username}</div> )} </div> ); } } export default App;
启动应用
$ npm start
在浏览器上访问localhost:3000,就能看到传递过来的用户列表了。