前言css
本文主要介绍了利用Vue.js+Node.js+MongoDB实现一个博客系统,这个博客使用Vue作前端框架,Node+express作后端,数据库使用的是MongoDB。实现了用户注册、用户登陆、博客管理、文章编辑、标签分类等功能html
实现的功能
前端
1.文章的编辑,修改,删除vue
2.支持使用 Markdown 编辑与实时预览node
3.支持代码高亮webpack
4.给文章添加标签git
5.支持用户注册登陆github
使用到的技术web
前端vue-router
1.Vue.js
2.vue-cli
3.vue-router
4.vue-resource
5.element-ui
6.marked
7.highlight.js
后端
1.Node.js
2.Express
3.Mongoose
基本思路
前端使用 vue-router 操做路由,实现单页应用的效果。使用 vue-resource 从后台获取数据,数据的处理所有都在前端,因此后端要作的事情很简单——把前端打包好的数据存进数据库中和从数据库中取出数据。先后端使用统一的路由命名规则。
项目目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
| app.js 后端入口
| index.html 入口页面
| .babelrc babel配置
| .gitignore git配置
| package.json
| webpack.config.js webpack配置
|
|-dist vue打包生成的文件
|
|-node_modules 模块
|
|-server 后端
| check.js
| db.js 数据库
__| router.js 路由
|
|-src 前端
|-assets 静态资源
|-components 组件
| App.vue
| main.js
|
webpack 配置
webpack 大部分是 vue-cli 自动生成的,添加了让先后端http请求都转到node的3000端口,而不是前端的8080端口的配置。
1
2
3
4
5
6
7
8
9
10
11
|
devServer: {
historyApiFallback:
true
,
noInfo:
true
,
//让先后端http请求都转到node的3000端口,而不是前端的8080端口
proxy: {
'/'
: {
}
}
}
|
这里涉及一个新手可能会不明白的问题(我以前就捣鼓了半天)。
开发的时候要先打开数据库 MongoDB ,使用命令 mongod。
而后打开后端服务器 node app,后端监听 3000 端口。
最后打开前端开发模式 npm run dev,前端启动了一个 webpack 服务器,监听 8080 端口用于热刷新。经过配置把前端的http请求转到 3000 端口。
前端部分
命名视图
全部页面都用到的元素能够写在 App.vue 上面,也能够写成公共组件。我在 App.vue 中使用了命名视图,由于 sidebar 这个组件有的页面须要有的不须要,不须要的时候就不用加载。
1
2
3
4
5
6
7
8
9
10
|
<!--App.vue-->
<template>
<div id="app">
<div class="black_line"></div>
<div id="main">
<router-view name="sidebar"></router-view>
<router-view></router-view>
</div>
</div>
</template>
|
router
路由的配置写在 main.js 中,分为前台展现和后台管理。后台管理统一以 ‘/admin' 开头。注册页和登陆页写在一块儿了,上面有两个按钮“注册”和“登陆”(我好懒-_-)。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// main.js
const router =
new
VueRouter({
routes: [
{path:
'/'
, components: {
default
: article, sidebar: sidebar}},
{path:
'/article'
, components: {
default
: article, sidebar: sidebar}},
{path:
'/about'
, components: {
default
: about, sidebar: sidebar}},
{path:
'/articleDetail/:id'
, components: {
default
: articleDetail, sidebar: sidebar}},
{path:
'/admin/articleList'
, components: {
default
: articleList, sidebar: sidebar}},
{path:
'/admin/articleEdit'
, component: articleEdit},
{path:
'/admin/articleEdit/:id'
, component: articleEdit},
{path:
'/admin/signin'
, component: signin}
]
})
|
element UI
使用了 element 用于消息提醒和标签分类。并不须要整个引入,而是使用按需引入。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// main.js
// 按需引用element
import { Button, Message, MessageBox, Notification, Popover, Tag, Input } from
'element-ui'
import
'element-ui/lib/theme-default/index.css'
const components = [Button, Message, MessageBox, Notification, Popover, Tag, Input]
components.forEach((item) => {
Vue.component(item.name, item)
})
const MsgBox = MessageBox
Vue.prototype.$msgbox = MsgBox
Vue.prototype.$alert = MsgBox.alert
Vue.prototype.$confirm = MsgBox.confirm
Vue.prototype.$prompt = MsgBox.prompt
Vue.prototype.$message = Message
Vue.prototype.$notify = Notification
|
vue-resource
用于向后端发起请求。打通先后端的关键。
1
2
3
4
5
6
|
// GET /someUrl
this
.$http.get(
'/someUrl'
).then(response => {
// success callback
}, response => {
// error callback
});
|
get 请求
前端发起 get 请求,当请求成功被返回执行第一个回调函数,请求没有被成功返回则执行第二个回调函数。
1
2
3
4
|
this
.$http.get(
'/api/articleDetail/'
+ id).then(
response =>
this
.article = response.body,
response => console.log(response)
)
|
后端响应请求并返回结果
1
2
3
4
5
6
7
8
9
10
|
// router.js
router.get(
'/api/articleDetail/:id'
,
function
(req, res) {
db.Article.findOne({ _id: req.params.id },
function
(err, docs) {
if
(err) {
console.error(err)
return
}
res.send(docs)
})
})
|
post 请求
前端发起 post 请求,当请求成功被返回执行第一个回调函数,请求没有被成功返回则执行第二个回调函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
// 新建文章
// 即将被储存的数据 obj
let obj = {
title:
this
.title,
date:
this
.date,
content:
this
.content,
gist:
this
.gist,
labels:
this
.labels
}
this
.$http.post(
'/api/admin/saveArticle'
, {
articleInformation: obj
}).then(
response => {
self.$message({
message:
'发表文章成功'
,
type:
'success'
})
// 保存成功后跳转至文章列表页
self.refreshArticleList()
},
response => console.log(response)
)
|
后端存储数据并返回结果
1
2
3
4
5
6
7
8
9
10
11
|
// router.js
// 文章保存
router.post(
'/api/admin/saveArticle'
,
function
(req, res) {
new
db.Article(req.body.articleInformation).save(
function
(err) {
if
(err) {
res.status(500).send()
return
}
res.send()
})
})
|
后端部分
后端使用 express 构建了一个简单的服务器,几乎只用于操做数据库。
app.js 位于项目根目录,使用 node app 运行服务器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
const express = require(
'express'
)
const fs = require(
'fs'
)
const path = require(
'path'
)
const bodyParse = require(
'body-parser'
)
const session = require(
'express-session'
)
const MongoStore = require(
'connect-mongo'
)(session)
const router = require(
'./server/router'
)
const app = express()
const resolve = file => path.resolve(__dirname, file)
app.use(
'/dist'
, express.static(resolve(
'./dist'
)))
app.use(bodyParse.json())
app.use(bodyParse.urlencoded({ extended:
true
}))
app.use(router)
// session
app.set(
'trust proxy'
, 1)
// trust first proxy
app.use(session({
secret:
'blog'
,
resave:
false
,
saveUninitialized:
true
,
cookie: {
secure:
true
,
maxAge: 2592000000
},
store:
new
MongoStore({
})
}))
app.get(
'*'
,
function
(req, res) {
let html = fs.readFileSync(resolve(
'./'
+
'index.html'
),
'utf-8'
)
res.send(html)
})
app.listen(3000,
function
() {
console.log(
'访问地址为 localhost:3000'
)
})
|
给本身挖了一个坑。由于登陆以后须要保存用户状态,用来判断用户是否登陆,若是登陆则能够进入后台管理,若是没有登陆则不能进入后台管理页面。以前写 node 的时候用的是 session 来保存,不过spa应用不一样于先后端不分离的应用,我在前端对用户输入的帐号密码进行了判断,若是成功则请求登陆在后端保存 session。不过不知道出于什么缘由,session 老是没办法赋值。由于我 node 学的也是半吊子,因此暂时放着,等我搞清楚了再来填坑。
收获
1.学一个新模块,新框架第一步就是阅读官方文档。
2.不要以为读文档费时间,认真的读一遍官方文档比你瞎折腾来得有效率。
3.阅读与你项目相关的优秀项目的源码,学习别人如何组织代码。
4.本身的解决方案不必定是最优解,不过在找到最优解以前不妨本身先试试。
5.框架模块的使用都不难,套API的活每一个人都能干,只是快与慢的差异。
6.尝试思考这个API是如何实现的。
7.了解了完整的web应用是如何运做的,包括服务器,数据库,前端是如何联系在一块儿的。