创建React App + Express 后台的应用
发布时间:2020-12-15 07:16:58 所属栏目:百科 来源:网络整理
导读:创建express app 全局安装 express-generator $ npm install -g express-generator 创建express app $ express react-backend 安装依赖包 $ npm install 修改react-backend/routes/users.js,返回简单的数据,方便测试 var express = require('express');var
创建express app
$ npm install -g express-generator
$ express react-backend
$ npm install
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;
$ PORT=3001 node bin/www 把端口设置成3001的原因是因为react app 会使用3000端口,避免冲突 创建react app
$ npm install -g create-react-app
# 在这里安装的时候 真心很慢,回头我研究一下,改成自己创建react应用,应该会快一点 $ create-react-app client
$ 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 是你后台服务器的地址
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,就能看到传递过来的用户列表了。 参考链接:https://daveceddia.com/create... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容