mongodb react
前端1
2
3
4
5cnpm install -g create-react-app
yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
npm config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
yarn create react-app reactlearn
cnpm install redux --save
1 | cnpm install express --save |
新建server文件夹
新建server.js1
2
3
4
5
6
7
8
9
10
11
12const app = express()
app.get('/',function (req,res) {
res.send('<hi>Hello word</hi>')
})
app.get('/data',function (req,res) {
res.json({name:'小明'})
})
app.listen(9093,function () {
console.log("输入正确")
})
运行:node server.js
访问9093成功
node 热加载1
2npm install -g nodemon`
nodemon server.js
mongodb1
2
3
4
5
6
7
8
9
10
11
12
13
14sudo yum install -y mongodb-org
whereis mongod
service mongod start
use admin
db.createUser(
{
user: "root", //用户名
pwd: "123456", //密码
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] //设置权限
}
)
vi /etc/mongod.conf
service mongod restart
mongo -u root -p 123456
node mongodb1
cnpm install mongoose --save
暂时先关掉auth
service1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45const mongoose = require('mongoose')
// 连接数据库
const DB_URL = 'mongodb://10.1.18.20:27017/reactlearn'
mongoose.connect(DB_URL)
mongoose.connection.on('connected',function () {
console.log("成功连接mongodb")
})
// 定义文档模型
const User = mongoose.model('user',new mongoose.Schema({
name: {type:String,requier:true},
age: {type:Number,requier:true}
}))
// 添加数据
User.create({
name:'wangwu',
age:18
},function (err,doc) {
if(!err){
console.log(doc)
}else{
console.log(err)
}
})
// 删除数据
User.remove({age:18},function (err,doc) {
if(!err){
console.log(doc)
User.find({},function (e,d) {
console.log(d)
})
}
})
// 更新
User.update({'name':'zhangsan'},{'$set':{age:26}},function (err, doc) {
console.log(doc)
})
const app = express()
// 查询
app.get('/data',function (req,res) {
User.find({"age":26},function (err,doc) {
res.json(doc)
})
})
antd 和组件
1 | cnpm install antd-mobile |
在 package.json babel加上1
2
3"plugins": [
["import", { "libraryName": "antd-mobile", "style": "css" }]
]
可以删掉import css
报错1
cnpm install react-dev-utils --save-dev
坑:
react里class名字要大写,小写tag会被当成html标签1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33class One extends Component{
constructor(props){
super(props)
this.state = {
arr:["a","b","c"]
}
// this.additem = this.additem.bind(this)
}
// additem = () =>{
additem(){
this.setState(
{
arr:[...this.state.arr,"aaa"+Math.random()]
}
)
}
render(){
const boss2 = "boss1"
return (
<div>
<ul>
{this.state.arr.map(v=>{
return <li key={v}>{v}</li>
})}
</ul>
<h2>boss1:,{this.props.boss1}</h2>
<button onClick={()=>this.additem()}>add users</button>
</div>
)
}
}
<button onClick={this.additem}>add users</button>
方法不能带小括号
方法的this绑定在构造函数里,或者用箭头函数<button onClick={()=>this.additem()}>add users</button>
redux和异步redux
1 | // index.redux.js |
1 | //index.js |
1 | //App.js |
thunk中间件1
cnpm install redux-thunk --save
1 | //index.js |
1 | // index.redux.js |
1 | const addAsync = this.props.addAsync |
目录结构
1 | /app |
搭建server
1 | npm install express-generator -g |
构建工具
文件合并,脚本编译,模板自动更新
创建命令行工具
生成依赖文件package.json
npm init
bebal配置
1 | .babelrc |
gulp 配置文件
1 | gulpfile.babel.js |
args.js
1 | // 命令行参数包 |