Skip to content

Commit

Permalink
完善博客系统
Browse files Browse the repository at this point in the history
  • Loading branch information
dunizb committed Oct 10, 2016
1 parent eaf5093 commit fcb6068
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 277 deletions.
1 change: 0 additions & 1 deletion nodejs/blog/README.md

This file was deleted.

4 changes: 4 additions & 0 deletions nodejs/blog/controllers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ router.get('/index', (req, res) => {
if (!req.session.userinfo) {
return res.redirect('/sign.html')
}
console.log(000);
Post.findCount((err, count) => {
if (err) throw err
res.locals.count = Math.ceil(count / 10)
console.log(11);
// 1.拿数据
Post.findLimit(1, 10, (err, posts) => {
console.log(222);
// 2.渲染模板
res.locals.user = req.session.userinfo
res.locals.posts = posts
Expand Down Expand Up @@ -81,6 +84,7 @@ router.post('/post/:id', (req, res) => {
// })
post.delete((err, result) => {
if(err) throw err
console.log(result);
result ? res.send({ err_msg: 'ok' }) :
res.send({ 'err_msg': 'err' })
})
Expand Down
10 changes: 5 additions & 5 deletions nodejs/blog/controllers/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ home.get('/index/:page' ,(req, res) =>{
// 调Model
// let posts = Post.findPage(page)
Post.findPage(page,function(posts){
console.log(posts)
// 拿到数据渲染模板,返回给用户
res.locals.posts = posts
res.locals.page = page
res.render('post') // 把模板转换为html字符返回
console.log(posts)
// 拿到数据渲染模板,返回给用户
res.locals.posts = posts
res.locals.page = page
res.render('post') // 把模板转换为html字符返回
})
console.log(121);

Expand Down
17 changes: 9 additions & 8 deletions nodejs/blog/controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const User = require('../models/user.js')
const Post = require('../models/post.js')
const Comment = require('../models/comment.js')
console.log(User);

// 设置前缀
router.prefix = '/'
Expand All @@ -34,9 +35,9 @@
if(err) throw err
if(!post) return next()
// 通过模板引擎返回博客的数据
res.locals.post = post
console.log(post);
res.render('postdetails')
res.locals.post = post
console.log(post);
res.render('postdetails')
})
})
})
Expand All @@ -61,10 +62,10 @@
if(err) throw err
if(!post) return next()
// 通过模板引擎返回博客的数据
res.locals.post = post
console.log(post);
res.send(post)
// res.render('postdetails')
res.locals.post = post
console.log(post);
res.send(post)
// res.render('postdetails')
})
})
})
Expand All @@ -75,7 +76,7 @@
*/
router.get('/comments', (req, res,next) =>{
// 1.调用评论对应的model,得到数据
// 404不是错误
// 404不是错误
Comment.find((err, comments) =>{
if(err) throw err
if(!comments) return next()
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions nodejs/blog/controllers/sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

// 登陆注册
const express = require('express')
const sign =module.exports= express.Router()

// 指定访问的前缀
sign.prefix = '/sign'

// action
sign.get('/in', (req, res) =>{
res.send('登陆成功')
})


sign.get('/out', (req, res) =>{
res.send('注销成功')
})
68 changes: 54 additions & 14 deletions nodejs/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
const express = require('express')
const app = express()

//

// 处理请求参数
const bodyParser = require('body-parser')

// 设置项目中使用的模板引擎
// 设置模板引擎所在目录,设置之后在render可以直接使用模板引擎的名字
Expand All @@ -19,12 +21,34 @@ app.set('view engine','xtpl')
// glob ,用于匹配不同类型的文件.
const glob = require('glob')

// 路径处理
const path = require('path')

// session
const session = require('express-session')



// 加入bodyParser中间件
app.use(bodyParser.urlencoded())
app.use(bodyParser.json())

// 引入一些中间件
// app.use
// 处理静态资源
// 处理静态资源,也可以称之为路由 /account/sign
app.use(express.static(path.join(__dirname,'www')))
// var require

// 设置session中间件
// app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
// cookie: { secure: true }
cookie:{maxAge:200000}
}))

// 动态网站/开头表示网站根目录
// /assets/css/sb-admin.css
// src('')
Expand All @@ -33,22 +57,38 @@ app.use(express.static(path.join(__dirname,'www')))

// 引入所有控制器 3.0方式 **
glob.sync('./controllers/*.js').forEach((item) => {
// item就是匹配到的指定规则的文件的路径
const tmp = require(item)
// 得到文件名
// const prefix = path.basename(item,'.js')

// /home
// app.use('/' + prefix ,tmp)
// console.log(item.prefix);
// console.log(tmp);
app.use(tmp.prefix, tmp)
// item就是匹配到的指定规则的文件的路径
const tmp = require(item)
// 得到文件名
// const prefix = path.basename(item,'.js')

// /home
// app.use('/' + prefix ,tmp)
// console.log(item.prefix);
// console.log(tmp);
// /account/sign
app.use(tmp.prefix, tmp)

})

// 统一处理404
app.use((req, res, next) => {
res.status(404)
//.send('我是404')
// res.render('')
// 跳转到新页面,让客户端重新请求一个指定的页面
// 其实是设置了Location:/404.html这个响应头.
res.redirect('/404.html')
// res.writeHeader(302,'Location','/404.html')
})

// 统一处理服务器端错误
// app.use((err ,req, res, next) =>{
// res.status(500).send('服务器错误了')
// })

// 监视服务
app.listen(3001, (err) => {
if(err) throw err
console.log('http://127.0.0.1:3001');
if(err) throw err
console.log('http://127.0.0.1:3001');
})
10 changes: 5 additions & 5 deletions nodejs/blog/models/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const mysql = require('mysql')

// 创建连接池
const pool = mysql.createPool({
connectionLimit:10, // 设置连接池里数据库连接的个数,
host : 'localhost', // 如果是远程服务器,就写成服务器ip
user : 'root', // 数据库用户名
password : '123456', // 数据库密码
database : 'blog'
connectionLimit:100, // 设置连接池里数据库连接的个数,
host : 'localhost', // 如果是远程服务器,就写成服务器ip
user : 'root', // 数据库用户名
password : '123456', // 数据库密码
database : 'blog'
})

module.exports = {
Expand Down
55 changes: 38 additions & 17 deletions nodejs/blog/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
const db = require('./db.js')

class Post{
// 当我们new Post(title,author)时执行constructor
constructor(id,slug,title,excerpt,content,type,status,comment_status,comment_count,view_count,created,modified,user_id,parent_id){
// 当我们new Post(title,author)时执行constructor
constructor(id,slug,title,excerpt,content,type,status,comment_status,comment_count,view_count,created,modified,user_id,parent_id){
// super() // 如果继承某个父级,就必需加上super(),如果不也constructor这个函数,就可以不加super()
this.id = id
this.slug = slug
this.title = title
this.excerpt = excerpt
this.content = content
this.type = type
this.status = status
this.comment_status = comment_status
this.comment_count = comment_count
this.view_count = view_count
this.created = created
this.modified = modified
this.user_id = user_id
this.parent_id = parent_id
}
this.id = id
this.slug = slug
this.title = title
this.excerpt = excerpt
this.content = content
this.type = type
this.status = status
this.comment_status = comment_status
this.comment_count = comment_count
this.view_count = view_count
this.created = created
this.modified = modified
this.user_id = user_id
this.parent_id = parent_id
}

// 获取文章列表 , static find(){} 是定义一个find静态方法
static find(){
Expand Down Expand Up @@ -64,6 +64,27 @@ const db = require('./db.js')
// forEach(function(item){
// return item
// })




// let tmpObj = obj
// 深拷贝

// page=1, 0 0,1,2,3,4
// page=2, 5 5,6,7,8,9
// page=3, 10 10,11,12,13,14
// start = (page-1)*pageSize
// // arr 1,2,3,4,5,6,7,8,9,10 3
// // page = 1: 1,2,3 []
// // let arr = obj.splice(0,3)
// // console.log(obj.length);
// // concat 拼接数据
// // Object.assign
// let tmpObj = [].concat(obj,[])
// console.log(tmpObj); //
// let arr = tmpObj.splice(start,pageSize)
// return arr
}

// 获取分页数据2
Expand Down
6 changes: 3 additions & 3 deletions nodejs/blog/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class User {
'insert into users set ?'
db.query(sql,[this , this.id] , (err, result) =>{
if(result.affectedRows>0){
this.id = this.id || result.insertId
callback(null,true)
this.id = this.id || result.insertId
callback(null,true)
}else{
callback(null ,false)
callback(null ,false)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions nodejs/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"glob": "^7.1.0",
"mysql": "^2.11.1",
Expand Down
48 changes: 0 additions & 48 deletions nodejs/blog/sqlscript/comments.sql

This file was deleted.

27 changes: 0 additions & 27 deletions nodejs/blog/sqlscript/initSql.js

This file was deleted.

Loading

0 comments on commit fcb6068

Please sign in to comment.