提交后端代码

master
liuyx 2 years ago
parent dfdcd58d8c
commit ea631f49d3

@ -1,12 +1,9 @@
// const express = require('express')
// const bodyparse = require('body-parser')
// const routes = require('./routes')
import express from 'express'
import bodyparse from 'body-parser'
import routes from './routes/index.js'
import mongoose from 'mongoose'
mongoose.connect('mongodb://localhost/test', (err) => {
mongoose.connect('mongodb://localhost/blog_test', (err) => {
if (err) {
console.log('error')
} else {

@ -1,20 +1,17 @@
/*
七牛云配置
*/
// const qiniu = require('qiniu')
import qiniu from 'qiniu'
// 创建上传凭证
const accessKey = 'et7Cr4yqTf5kHJ3cNuAkzAghjGI378X9tflGO6dT' // 这里填写七牛云的accessKey
const secretKey = '1eKk1NJIeXmuZ6klkvaLmvRqVQeuO817G_6AOblD'// 这里填写七牛云的secretKey
const secretKey = '1eKk1NJIeXmuZ6klkvaLmvRqVQeuO817G_6AOblD' // 这里填写七牛云的secretKey
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const options = {
scope: 'liuyxcc-blog', // 这里填写七牛云空间名称
expires: 60 * 60 * 24 * 7
expires: 60 * 60 * 24 * 356
}
const putPolicy = new qiniu.rs.PutPolicy(options)
const uploadToken = putPolicy.uploadToken(mac)
export {
uploadToken
}
export { uploadToken }

@ -0,0 +1,7 @@
import mongoose from 'mongoose'
const AboutSchema = new mongoose.Schema({
content: String
})
export default mongoose.model('About', AboutSchema, 'about')

@ -2,25 +2,18 @@
import mongoose from 'mongoose'
const ArticleSchema = new mongoose.Schema({
cover: {
type: String
},
cover: String,
title: String,
content: String,
category: String,
tagList: [],
tagList: Array,
views: {
type: Number,
default: 0
},
createTime: {
type: Date,
default: Date.now()
},
updateTime: {
type: Date,
default: Date.now()
}
createTime: String,
updateTime: String,
about: String
})
export default mongoose.model('Article', ArticleSchema, 'article')

@ -0,0 +1,11 @@
import mongoose from 'mongoose'
const CategorySchema = new mongoose.Schema({
name: String,
articleNum: {
type: Number,
default: 0
}
})
export default mongoose.model('Category', CategorySchema, 'category')

@ -0,0 +1,8 @@
import mongoose from 'mongoose'
const PageSchema = new mongoose.Schema({
pageName: String,
bannerUrl: String
})
export default mongoose.model('Page', PageSchema, 'page')

@ -0,0 +1,22 @@
/*
* @Author: liuyx 1517482303@qq.com
* @Date: 2022-12-17 08:19:03
* @LastEditTime: 2022-12-17 09:38:01
* @Description: 网站信息Schema
*/
import mongoose from 'mongoose'
const SiteSchema = mongoose.Schema({
avatar: String,
siteName: String,
author: String,
intro: String,
sentence: String,
date: Date,
notice: String,
github: String,
gitee: String,
icp: String
})
export default mongoose.model('Site', SiteSchema, 'site')

@ -0,0 +1,11 @@
import mongoose from 'mongoose'
const TagSchema = new mongoose.Schema({
name: String,
articleNum: {
type: Number,
default: 0
}
})
export default mongoose.model('Tag', TagSchema, 'tag')

File diff suppressed because it is too large Load Diff

@ -16,6 +16,12 @@
"qiniu": "^7.8.0"
},
"devDependencies": {
"eslint": "^8.29.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.6.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-vue": "^9.8.0",
"nodemon": "^2.0.20"
}
}

@ -0,0 +1,33 @@
import express from 'express'
import About from '../models/about.js'
import result from '../utils/result.js'
const aboutRouter = express.Router()
aboutRouter.get('/', async (req, res) => {
const about = await About.findOne()
console.log(about)
if (about === null) {
res.status(200).json(result(400, '暂时没有数据哦', { _id: '', content: '' }))
} else {
res.status(200).json(result(200, '查询成功', about))
}
})
aboutRouter.put('/', async (req, res) => {
const data = req.body
if (data._id === '') {
const about = new About({
content: data.content
})
await about.save()
res.status(200).json(result(200, '创建成功', null))
} else {
await About.updateOne({ _id: data._id }, data)
res.status(200).json(result(200, '更新成功', null))
}
// await About.updateOne()
console.log(data)
})
export default aboutRouter

@ -6,13 +6,12 @@ const articleRouter = express.Router()
// 获取所有文章
articleRouter.get('/', async (req, res) => {
console.log(req.query)
const { currentPage, pageSize } = req.query
const articleList = await Article.find({})
.skip((currentPage - 1) * pageSize)
.limit(pageSize)
.sort({ createTime: 'desc' })
const count = await Article.countDocuments({})
console.log(count)
res.status(200).json(result(200, '查询成功', { articleList, count }))
})
@ -37,7 +36,9 @@ articleRouter.post('/', async (req, res) => {
title: data.title,
content: data.content,
tagList: data.tagList,
category: data.category
category: data.category,
createTime: data.createTime,
updateTime: data.updateTime
})
await article.save()
res.status(200).json(result(200, '发布成功', null))

@ -0,0 +1,41 @@
import express from 'express'
import Category from '../models/category.js'
import result from '../utils/result.js'
const categpryRouter = express.Router()
// 获取所有标签
categpryRouter.get('/', (req, res) => {
Category.find({}, (err, doc) => {
if (err) {
res.status(500).json(result(500, '查询出错啦', null))
}
res.status(200).json(result(200, '查询成功', doc))
})
})
// 根据id获取标签
categpryRouter.get('/:id', async (req, res) => {
const category = await Category.findOne({ _id: req.params.id })
res.status(200).json(result(200, null, category))
})
// 根据id是否为空字符串来实现新增或修改
categpryRouter.post('/', async (req, res) => {
const data = req.body
if (data._id === '') {
await Category.create({ name: data.name })
res.status(200).json(result(200, '新增成功', null))
} else {
await Category.updateOne({ _id: data._id }, { name: data.name })
res.status(200).json(result(200, '修改成功', null))
}
})
// 根据id删除标签
categpryRouter.delete('/:id', async (req, res) => {
await Category.deleteOne({ _id: req.params.id })
res.status(200).json(result(200, '删除成功', null))
})
export default categpryRouter

@ -1,16 +1,25 @@
// const token = require('./token')
// const article = require('./article')
/*
* @Author: liuyx 1517482303@qq.com
* @Date: 2022-11-23 10:44:22
* @LastEditTime: 2022-12-17 09:22:16
* @Description: 注册路由
*/
import token from './token.js'
import article from './article.js'
// module.exports = routers = (app) => {
// app.use('/token', token)
// app.use('/article', article)
// }
import tag from './tag.js'
import category from './category.js'
import site from './site.js'
import about from './about.js'
import page from './page.js'
const routes = (app) => {
app.use('/token', token)
app.use('/article', article)
app.use('/articles', article)
app.use('/tags', tag)
app.use('/categories', category)
app.use('/sites', site)
app.use('/about', about)
app.use('/pages', page)
}
export default routes

@ -0,0 +1,43 @@
import express from 'express'
import Page from '../models/page.js'
import result from '../utils/result.js'
const pageRouter = express.Router()
pageRouter.get('/', async (req, res) => {
if ((await Page.find()).length === 0) {
new Page({ pageName: '主页', bannerUrl: 'http://cdn.liuyx.cc/wallhaven-m3dm1k.png' }).save()
new Page({
pageName: '分类',
bannerUrl: 'https://typora-lyx.oss-cn-guangzhou.aliyuncs.com/typora/wallhaven-kxw9o1.png'
}).save()
new Page({
pageName: '标签',
bannerUrl: 'https://typora-lyx.oss-cn-guangzhou.aliyuncs.com/typora/wallhaven-kxw3p1.jpg'
}).save()
new Page({
pageName: '归档',
bannerUrl: 'https://typora-lyx.oss-cn-guangzhou.aliyuncs.com/typora/wallhaven-1p38v3.png'
}).save()
new Page({
pageName: '关于我',
bannerUrl: 'https://typora-lyx.oss-cn-guangzhou.aliyuncs.com/typora/wallhaven-p931d9.png'
}).save()
}
const pageList = await Page.find({})
res.status(200).json(result(200, '页面图片查询成功', pageList))
})
pageRouter.get('/:pageName', async (req, res) => {
const pageName = req.params.pageName
const page = await Page.findOne({ pageName })
res.status(200).json(result(200, '获取页面图片成功', page))
})
pageRouter.put('/', async (req, res) => {
const { _id, bannerUrl } = req.body
await Page.updateOne({ _id }, { bannerUrl })
res.status(200).json(result(200, '修改页面图片成功', null))
})
export default pageRouter

@ -0,0 +1,28 @@
/*
* @Author: liuyx 1517482303@qq.com
* @Date: 2022-12-17 09:14:28
* @LastEditTime: 2022-12-17 15:10:09
* @Description: 网站信息相关操作
*/
import express from 'express'
import Site from '../models/Site.js'
import result from '../utils/result.js'
const siteRouter = express.Router()
siteRouter.get('/', async (req, res) => {
const siteInfo = await Site.findOne()
if (siteInfo === null) {
res.status(500).json(result(500, '未能找到有关信息', null))
} else {
res.status(200).json(result(200, '查询成功', siteInfo))
}
})
siteRouter.put('/', async (req, res) => {
const data = req.body
await Site.updateOne({ _id: data._id }, data)
res.status(200).json(result(200, '更新成功', null))
})
export default siteRouter

@ -0,0 +1,44 @@
import express from 'express'
// import Article from '../models/article.js'
import Tag from '../models/tag.js'
import result from '../utils/result.js'
const tagRouter = express.Router()
// 获取所有标签
tagRouter.get('/', (req, res) => {
Tag.find({}, (err, doc) => {
if (err) {
res.status(500).json(result(500, '查询出错啦', null))
}
res.status(200).json(result(200, null, doc))
})
})
// 根据id获取标签
tagRouter.get('/:id', async (req, res) => {
const tag = await Tag.findOne({ _id: req.params.id })
res.status(200).json(result(200, null, tag))
})
// 根据id是否为空字符串来实现新增或修改
tagRouter.post('/', async (req, res) => {
const data = req.body
if (data._id === '') {
await Tag.create({ name: data.name })
res.status(200).json(result(200, '新增成功', null))
} else {
// const temp = await Article.find({ tagList: { $elemMatch: { _id: data._id } } })
// console.log(temp)
await Tag.updateOne({ _id: data._id }, { name: data.name })
res.status(200).json(result(200, '修改成功', null))
}
})
// 根据id删除标签
tagRouter.delete('/:id', async (req, res) => {
await Tag.deleteOne({ _id: req.params.id })
res.status(200).json(result(200, '删除成功', null))
})
export default tagRouter

@ -2,11 +2,12 @@
// const qnconfig = require('../config') // 引入七牛云配置
import express from 'express'
import { uploadToken } from '../config.js'
import result from '../utils/result.js'
const tokenRouter = express.Router()
tokenRouter.get('/qiniu', (req, res, next) => {
res.status(200).send(uploadToken)
res.status(200).send(result(200, 'null', uploadToken))
})
// module.exports = tokenRouter

Loading…
Cancel
Save