標籤:func string each ret blog oca 分享圖片 技術 ocs
github地址:https://github.com/linguowei/myblog
把項目git clone下來;
分析:
# git clone https://github.com/linguowei/myblog.git# cd myblog# npm install# npm run build# cd admin# npm run build#. cd ../# node app.js# localhost:7000# localhost:7000/admin
運行代碼;
這裡分別安裝依賴包,以及打包產生 後台項目以及前台項目
# node app 實現運行服務
app.js
其中與資料相連起來的是:
app.use(router)
資料庫連接+建立各種表;db.js
分別建立:帳號表;
文章表;
標籤表;
個人資訊表;
var mongoose = require(‘mongoose‘)mongoose.Promise = require(‘bluebird‘)// mongoose.connect(‘mongodb://wei1:[email protected]:61018/weiwei‘)mongoose.connect(‘mongodb://localhost:27017/weiweiblog‘)var userSchema = new mongoose.Schema({ name: String, pwd: String,})var articleSchema = new mongoose.Schema({ title: String, date: Date, articleContent: String, state: String, label: String,})var tagSchema = new mongoose.Schema({ tagName: String, tagNumber: Number,})var personalInformationSchema = new mongoose.Schema({ name: String, individualitySignature: String, introduce: String,})var Models = { User: mongoose.model(‘User‘, userSchema), Article: mongoose.model(‘Article‘, articleSchema), TagList: mongoose.model(‘TagList‘, tagSchema), PersonalInformation: mongoose.model(‘PersonalInformation‘, personalInformationSchema),}module.exports = Models
在 localhost:7000/admin 中分別建立文章,標籤,部落格描述;
實際背景操作:
啟動資料調試
cd / usr/local/mongodb/binsudo ./mongo
查看articles的文章
查看標籤
vue到node,moogooes ,mongodb的請求過程
這裡我拿儲存標籤來
前端發送請求的代碼:
網頁具體發生請求;
node+mongoose對資料的處理和操作;
後台對資料的處理;
// 文章標籤儲存路由router.post(‘/api/saveArticleLabel‘, function(req, res){ db.TagList.find({}, function(err, docs){ if(err)return; var isExist = false; //對資料進行遍曆; docs.forEach(function(item){ if(item.tagName == req.body.tagList.tagName){ isExist = true; } }) //存在的話不進行處理,並返回錯誤 if (isExist){ res.json({error: true, msg: ‘標籤已存在‘}) }else{ //存到資料庫 new db.TagList(req.body.tagList).save(function(error){ if (error) { res.send(‘儲存失敗‘); return } res.send() })
res.json({success: true, msg: ‘儲存成功‘})
} }) });
具體的使用流程就是這樣的,實際更多的api的使用請自行查看mongoose/express的文檔進行使用;
兩天的成果---node,express,mongodb,以及mongoose 對github搭建自己部落格的項目進行分析