preface
I completed 80% of my personal blog project in about 10 days, and there are only some optimization and content supplement left. It is time to summarize the project from 0. I am a front-end white, technical level is not high, but also hope to be accepted
Personal blog is not online yet, and the follow-up work is not completed. Here is the preview
The front desk style borrowed from the big guy’s design style www.ssevenk.com/
The back-end
Express and mongodb were selected as back-end interface service support.
Back-end file directory:
Admin is the background management static file, model is the database model, plugins are used to store mongoose connection database file, routes contains foreground and background interface, uploads is the image uploading folder, web is the foreground static file
Database design
The blog uses eight tables as data stores, They are background user table (AdminUser), article table (Type), Article table (Artcom), process table (time), message table (Word), message reply table (reword) and friend list (friend).
These tables, the three tables related to the article are associated, the message and reply message table are associated, and the rest are independent
Install Nodemon to debug Node
npm i -g nodemonCopy the code
Install express Mongoose CORS module to use Express framework, Mongoose connection tool, CORS cross domain
npm i express@next mongoose corsCopy the code
Create plugins folder for db.js to connect to database and export with module.exports
module.exports=app=>{ const mongoose=require('mongoose') mongoose.set('useCreateIndex', True) / / solve the problem of error mongoose. Connect (' mongo: / / localhost: 27017 / blogs', {useNewUrlParser: true})}Copy the code
The data structures are respectively
const mongoose=require('mongoose')const schema=new mongoose.Schema({
username:{type:String},
password:{type:String,set(val){ return require('bcryptjs').hashSync(val,10) }}})
module.exports=mongoose.model('AdminUser',schema)Copy the code
const mongoose = require('mongoose') const schema = new mongoose.Schema({// Article type nametypeName: { type: String}}, {// add toJSON toJSON:{virtuals:true}})
schema.virtual("articles",
{ ref:"Article".localField:'_id',
foreignField:'type_id',
justOne:false})
module.exports=mongoose.model('Type',schema)Copy the code
const mongoose = require('mongoose') const schema = new mongoose.Schema({// article type id type_id: {type: mongoose.SchemaTypes.ObjectId,ref:'Type'}, // title: {type: String}, // article_content: {type: String}, // introduce: {type: String}, // viewcount: {type: Number },
LocalTime:{ type:String },
dzsj:{ type: Number }}
,{ timestamps:true})
module.exports=mongoose.model('Article',schema)Copy the code
const mongoose=require('mongoose')
const schema=new mongoose.Schema({
name:{type:String},
img:{type:String},
content:{type:String},
artid:{type:mongoose.SchemaTypes.ObjectId,ref:'Article'},
time:{type:String}}
,{ timestamps:true})
module.exports=mongoose.model('Artcom',schema)Copy the code
const mongoose=require('mongoose')
const schema=new mongoose.Schema({
name:{type:String},
content:{type:String},
headIndex:{type:Number},
time:{type:String}},
{ timestamps:true})
module.exports=mongoose.model('Word',schema)Copy the code
const mongoose=require('mongoose')
const schema=new mongoose.Schema({
name:{type:String},
content:{type:String},
headIndex:{type:Number},
time:{type:String}},
{ timestamps:true})
module.exports=mongoose.model('Word',schema)Copy the code
Only important structures, processes and links are posted
Express is introduced at the back end of index.js, listening on port 3000
const express=require('express')const app=express() // cross-app app. Use (require()'cors'() ()) // database require('./plugins/db'Json middleware app.use(express.json()) // require('./routes/web/index')(app)
require('./routes/admin/index')(app)
app.listen(3000,function(){
console.log('http://localhost:3000'})// Listen on port 3000Copy the code
Create a routes folder and create admin and Web folders inside to handle foreground and background routing, respectively
interface
Write the routing interface in routes/admin/index.js
module.exports = app => {
const router = require('express').Router()
const AdminUser = require('.. /.. /model/AdminUser')
app.use('/admin/api', router)}Copy the code
Use Mongoose API to realize user’s add, delete, change and check
router.post('/adminuser', async (req, res) => {
console.log(req.body)
const user = await AdminUser.create(req.body)
res.send(user) })
router.put('/adminuser/:id', async (req, res) => {
const model = await AdminUser.findByIdAndUpdate(req.params.id, req.body)
res.send(model) })
router.get('/adminuser/:id', async (req, res) => {
const model = await AdminUser.findById(req.params.id)
res.send(model) })
router.get('/adminuser', async (req, res) => {
const model = await AdminUser.find()
res.send(model) })Copy the code
There are other additions, deletions, changes and checks, which are roughly the same, but omitted