
talkIsCheap( )? goto(lastParagraph):continue( )
全栈工程师,也叫全端工程师(同时具备前端和后台能力),英文 Full Stack developer。是指掌握多种技能,并能利用多种技能独立完成产品的人. ( From 百度)
敲黑板:利用多种技能独立完成产品。
So, 至少需要一个人 cover 掉 前端+ 后端 + 数据库吧 ~
搞一个活生生的投票应用送给各位 : )
具体功能为:
对于已经授权用户可以:
非授权用户只能:
整体技术目标:React + Express + Mongodb 完成一个投票 SPA
概述
前端入口 (View source)
利用 React router 将首页指向 Home 组件,定义三个新路由 list、detail、new,分别指向对应页面组件。
<Router key={Math.random()} history={browserHistory} > <Route path="/" compOnent={App}> <IndexRoute compOnent={Home}/> <Route path="/list(/:name)" compOnent={List}> </Route> <Route path="/detail(/:id)" compOnent={Detail}> </Route> <Route path="/new" compOnent={New}></Route> </Route> </Router> 页面级组件 每一个页面级组件对应一个应用页面,均继承自 React.Component
//list(/:name)/detail(/:id)/new公共组件
header (View source): 所有页面的共同头部,包含个性化 menu、用户头像姓名,登入登出按钮等等
footer (View source): 可复用的页面底部,包含 copyright 信息
loading: 加载动效 (推荐一个工具 loading.io)
spning: 按钮等待动效
异步请求
概述
express.Router(),配合前端 React-router。处理外部直接 Landing、原地重刷新以及区分 404 页面:Views
React.reader() 的目标 DOM 节点<head> 信息,以及在其中需要同步加载的 js、css 资源Views Router (View source)
所有 Landing 或者页面刷新的链接都由后端同一收口到 views 中的 index,再由前端统一处理。
router.get('/detail/:id', throwToHome); router.get('/detail', throwToHome); router.get('/list/:name', throwToHome); router.get('/list', throwToHome); router.get('/new', throwToHome); router.get('/', throwToHome); router.get('*', throwToError); function throwToHome(request, response){ response.render('index', { cdnUrl: config.CDN_URL } ); } function throwToError(request, response){ response.render('error', { cdnUrl: config.CDN_URL } ); } 第三方用户登陆
API (View source)
/getPollList: 获取 polls 列表 userName (用户名),可获取该用户创建的所有 polls; 缺省 userName 则获取所有存在的 polls/getPollByID: 获取指定的 poll 的详细信息 pollID (poll 唯一标识)/upDatePollByID: 更新一个 poll (投票) pollID (poll 唯一标识); index(用户具体投给的选项编号); voter(传递投票的用户名,避免用户对同一个 poll 多次投票)result: bool 类型值,告诉你是否更新成功/insertPoll: 新建一个 poll title(poll 题目),description(poll 描述),options: (poll 中的选项),ownerName(创建用户), voterList(投票用户,初始为空数组)result: bool 类型值,告诉你是否创建成功Mongodb MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
环境安装和配置
.bashrc文件:export PATH=<mongodb-install-directory>/bin:$PATH, 例如我的是 export PATH=$HOME/mongodb/bin:$PATH mongod 运行 Cli, 用命令行对于数据库的常用操作
mongo // 进入 Cli show dbs // 显示所有数据库 use <dbName> // 进入指定数据库 show collections // 显示所有 collections (table) db.<collectionName>.find({}) // 查询 db.<collectionName>.deleteOne({}) // 删除 Express 连接 Mongodb (View source):
var dbUrl = 'mongodb://localhost:27017/voting' var mOngo= require("mongodb").MongoClient; mongo.connect(dbUrl, function(err, db){
var pullList = db.collection('pollList'); pullList.find({ownerName: ownerName},{}).sort({timestamp: -1}).toArray(function(err, docs){ if(err){ db.close(); errCal(err); } else { db.close(); sucCal(docs); } }); });
数据库 collection (table) 设计 userList schema:
{ "type": "array", "items": { "type": "object", "properties": { "_id": { "type": "integer" }, "email": { "type": "string" }, "name": { "type": "string" }, "timestamp": { "type": "integer" } } } } pollList schema:
{ "type": "array", "items": { "type": "object", "properties": { "_id": { "type": "integer" }, "description": { "type": "string" }, "options": { "type": "array", "items": { "type": "object", "properties": { "count": { "type": "integer" }, "index": { "type": "integer" }, "option": { "type": "string" } } } }, "ownerName": { "type": "string" }, "pollID": { "type": "string" }, "title": { "type": "string" }, "voterList": { "type": "array", "items": { "type": "string" } } } } } 官网 (可能需要梯子)
cd WeVoting heroku login 进行登录,无账号则去官网申请heroku create <yourAppName> 创建 App,这里的appName 也是你在herokuapp.com 下的三级域名heroku create we-voting-ele 最终域名为: https://we-voting-ele.herokuapp.com git push heroku master heroku open web: node index.js heroku ps:scale web=1 一行命令部署 mLab MongoDB 免费版
heroku addons:create mongolab:sandbox 获取云数据库服务链接 Mongodb_uri, 用于后台代码连接 Mongodb 服务
heroku config 详情请见:Getting Started on Heroku with Node.js
Done ~

看完了就去 Git repository 加个星星吧 : ) 能互相 follow 就更好了 ~