兩天的成果---node,express,mongodb,以及mongoose 對github搭建本身博客的項目進行分析

github地址:https://github.com/linguowei/myblog前端

把項目git clone下來;vue

分析:node

# 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

運行代碼;git

這裏分別安裝依賴包,以及打包生成 後臺項目以及前臺項目github

# node app 實現運行服務mongodb

 

app.js數據庫

其中與數據相連起來的是:  express

app.use(router)npm

 

數據庫鏈接+創建各類表;db.jsjson

 

分別創建:帳號表;

               文章表;

               標籤表;

               我的信息表;

var mongoose = require('mongoose')
mongoose.Promise = require('bluebird')

// mongoose.connect('mongodb://wei1:0987654321@ds161018.mlab.com: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/bin

sudo ./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的文檔進行使用;

相關文章
相關標籤/搜索