项目作者: shiliangL

项目描述 :
react全家桶,更好的理解redux,用装饰器的写法使用 react-redux,socket-io即时通信
高级语言: JavaScript
项目地址: git://github.com/shiliangL/react-redux-chat.git
创建时间: 2018-11-03T15:01:51Z
项目社区:https://github.com/shiliangL/react-redux-chat

开源协议:MIT License

下载


手把手教你从前端到后台实现登录注册功能

使用react全家桶,更好的理解redux,用装饰器的写法使用 react-redux,socket-io即时通信

本文基于MacOS系统,基本的开发环境安装配置如果没有安装好请自行百度安装

—————前后端配置安装(MacOS)——————-

后台数据库mongoDB安装

使用homebrew安装mongodb

  • brew install mongodb
  • mongo —version 查看安装版本
  • 新版开箱即用,不用像之前要配什么DB文件了(系统环境可能需要配置一下)
  • 已默认配置启动 mongod —config /usr/local/etc/mongod.conf
  • mongo 图形界面
  • 推荐界面管理工具 RoBo3T

RoBo3T

  • 后台启动mongodb用工具成功连接数据库
后端基于node,express开发,后台代码-service文件夹中

本地后台服务开发调试 nodemon 启动后台(后台代码更新自动重启服务器),安装

  1. npm install -g nodemon

数据库mongodb,mongodb配合express使用需要插件mongoose

  1. npm i mongoose -S
  1. const mongoose = require('mongoose')
  2. const DB_URL = 'mongodb://127.0.0.1:27017/cmm'
  3. //链接数据库
  4. mongoose.connect(DB_URL)
  5. mongoose.connection.on('connected', () => {
  6. console.log('数据库链接成功')
  7. })

———————界面———————

———————前端开发———————

-安装依赖
-配置redeux开发调试

  1. import { Provider } from 'react-redux'
  2. import { createStore,applyMiddleware,compose } from 'redux'
  3. import thunk from 'redux-thunk'
  4. import App from './App';
  5. import { AppStore } from '@/store';
  6. let store = createStore(rootReducer,
  7. compose(
  8. applyMiddleware(thunk),
  9. window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  10. )
  11. )
  12. ReactDOM.render(
  13. <Provider store={store}>
  14. <App ></App>
  15. </Provider>,
  16. document.getElementById('root')
  17. );
  • 使用 connect 链接 组件与 状态中心, connect接收两个参数(其实有4个参数)

    1-映射数据 可以理解为 state
    2-映射方法

  1. import { connect } from "react-redux";
  2. @connect(
  3. (state) => (state.user),
  4. {
  5. userRegisger
  6. }
  7. )
  8. class Register extends Component {
  9. render(){
  10. return null
  11. }
  12. }
  13. 用装饰器的写法确实比之前的写法更简洁了

create-react-app 中使用装饰器

安装

  1. npm install --save-dev @babel/plugin-proposal-decorators
  2. 配置( npm run eject )
  3. "babel": {
  4. "presets": [
  5. "react-app"
  6. ],
  7. "plugins": [
  8. [
  9. "@babel/plugin-proposal-decorators",
  10. {
  11. "legacy": true
  12. }
  13. ],
  14. [
  15. "import",
  16. {
  17. "libraryName": "antd",
  18. "style": "css"
  19. }
  20. ]
  21. ]
  22. },

———————服务器后台———————

基于node,express开发

  • app.get,app.post,开发get和post接口
  • app.use使用模块
  • res.send;res.json;res.sendfile

mongodb配合express进阶

  • mongodb独立工具函数

  • express使用body-parser支持post参数

  • 使用cookie-parser储存登录信息cookie

密码 cmd5加密+加严 安装 npm i utility -S