兩天的成果---node,express,mongodb,以及mongoose 對github搭建自己部落格的項目進行分析

來源:互聯網
上載者:User

標籤: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搭建自己部落格的項目進行分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.