码迷,mamicode.com
首页 > 数据库 > 详细

两天的成果---node,express,mongodb,以及mongoose 对github搭建自己博客的项目进行分析

时间:2018-03-18 16:22:03      阅读:491      评论:0      收藏:0      [点我收藏+]

标签: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: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的文档进行使用;

 

两天的成果---node,express,mongodb,以及mongoose 对github搭建自己博客的项目进行分析

标签:func   string   each   ret   blog   oca   分享图片   技术   ocs   

原文地址:https://www.cnblogs.com/heyinwangchuan/p/8595470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!