Nodejs 前端博客,基于express4、mongoose、swig、howdo、mongoose-objectpath、mongoose-validatefilter

Nodejs 前端博客,基于express4、mongoose、swig、howdo、mongoose-objectpath、mongoose-validatefilter

#前端博客 http://qianduanblog.com/ 目前前端博客已完全由wordpress更新为nodejs,并保留了原有的文章、评论、图片等重要数据。从开始计划写Nodejs博客,到它的公开面世,已经过去几个月有余了。从0开始接触nodejs,到目前使用已基本熟练,一路走过来,学到了很多,也挖了很多的坑,同时也填了很多的坑。

#前端博客目前有以下特点:

  • 最基本的用户管理:用户分配有4个角色:游客(0)、订阅者(1)、作者(2)、管理员(3)、博主(4),其中博主只能并且只有1位。角色的分配参考于wordpress,但不会有wordpress那么细致。不同的角色之间,上级可以对下级进行适当操作,而下级对上级不能进行任何操作。
  • 最基本的文章管理:每篇文章都配有一个URI字段,用来修饰、语义化文章的URL,如你现在看到的这样。用户的角色也影响着文章的发布、编辑和删除功能。前台显示文章会根据文章的更新时间来显示不同的提示语句,以提示访客文章的内容可能略有出入,如有疑问请评论告知作者等语句。文章还依旧有回复可见设置,提高访客与作者的互动性。
  • 最基本的评论管理:评论只有父子2级关系,每篇文章都可以被评论。评论只有2种状态:审核和未审核。未审核的评论,也会在前台占位显示,只是评论作者、网址、头像和内容等信息都将被格式化,评论作者显示佚名,评论内容显示为未审核提示语句。并且评论也有黑名单管理,可以通过添加IP、邮箱、关键词等拦截条件。评论也有邮件通知,这些都是必备的。
  • 最基本的分类管理:博客初始化的时候,会生成一个默认分类,并且这个默认分类是不允许二次修改的。分类只有1级关系,每篇文章只能从属于一个分类。
  • 最基本的标签管理:每篇文章都必须填写标签,标签是影响文章的搜索的,也是文章的关键词。
  • 最基本的图片管理:可以批量上传图片,图片也可以二次在线修改尺寸。
  • 最基本的友链管理:可以添加任意数量的友情链接。
  • 最基本的个人中心:注册用户,即角色值大于0的用户都有一个个人中心后台页面,用来修改自己的个人信息,包括邮箱、密码、网址、公司、职位、授权登录等信息。
  • 最基本的博客设置:博客设置只能由博主操作,包括博客名称、标题、描述、邮件通知、博客统计、评论黑名单、评论关键词、默认头像等信息。
  • 最完善的授权登录:博客不开放直接注册功能,如需直接注册,需要管理及以上角色才能操作。但每个人都可以使用授权登录的方式注册,授权渠道包括:QQ互联、腾讯微博、新浪微博、Github、人人网、开心网、百度。

#前端博客的未来规划:

  • 更完美的前端交互和表现。前端页面目前仍在探索阶段,未来将会更漂亮。
  • 更完美的SEO和认知度。

3 回复

Node.js 前端博客:基于 Express 4, Mongoose, Swig, HowDo, Mongoose-ObjectPath 和 Mongoose-ValidateFilter

引言

前端博客 http://qianduanblog.com/ 目前已经完全由 WordPress 更新为 Node.js,并保留了原有的文章、评论、图片等重要数据。从开始计划写 Node.js 博客,到它的公开面世,已经过去了几个月的时间。从零开始接触 Node.js 到目前的熟练使用,一路上学到了很多,也填了很多的坑。

前端博客的特点

  1. 用户管理

    • 用户角色分配:游客(0)、订阅者(1)、作者(2)、管理员(3)和博主(4)。博主只能并且只有1位。
    • 示例代码:
      const mongoose = require('mongoose');
      const Schema = mongoose.Schema;
      
      const UserSchema = new Schema({
        username: { type: String, required: true },
        role: { type: Number, default: 0 }
      });
      
      module.exports = mongoose.model('User', UserSchema);
      
  2. 文章管理

    • 每篇文章都有一个 URI 字段,用于修饰和语义化文章的 URL。
    • 示例代码:
      const PostSchema = new Schema({
        title: { type: String, required: true },
        uri: { type: String, unique: true },
        content: { type: String, required: true },
        authorId: { type: Schema.Types.ObjectId, ref: 'User' },
        updated: { type: Date, default: Date.now }
      });
      
      module.exports = mongoose.model('Post', PostSchema);
      
  3. 评论管理

    • 评论只有父子两级关系,每篇文章都可以被评论。评论有审核和未审核两种状态。
    • 示例代码:
      const CommentSchema = new Schema({
        postId: { type: Schema.Types.ObjectId, ref: 'Post' },
        author: { type: String, default: '匿名' },
        content: { type: String, required: true },
        status: { type: String, enum: ['pending', 'approved'], default: 'pending' }
      });
      
      module.exports = mongoose.model('Comment', CommentSchema);
      
  4. 分类管理

    • 博客初始化时生成一个默认分类,并且该分类不允许二次修改。
    • 示例代码:
      const CategorySchema = new Schema({
        name: { type: String, required: true },
        slug: { type: String, unique: true }
      });
      
      module.exports = mongoose.model('Category', CategorySchema);
      
  5. 标签管理

    • 每篇文章都必须填写标签,标签影响文章的搜索。
    • 示例代码:
      const TagSchema = new Schema({
        name: { type: String, required: true },
        posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }]
      });
      
      module.exports = mongoose.model('Tag', TagSchema);
      
  6. 图片管理

    • 可以批量上传图片,图片可以二次在线修改尺寸。
    • 示例代码:
      const fs = require('fs');
      const path = require('path');
      
      app.post('/upload', (req, res) => {
        let file = req.files.file;
        file.mv(path.join(__dirname, 'uploads', file.name), err => {
          if (err) return res.status(500).send(err);
          res.send({ url: `/uploads/${file.name}` });
        });
      });
      
  7. 友链管理

    • 可以添加任意数量的友情链接。
    • 示例代码:
      const LinkSchema = new Schema({
        name: { type: String, required: true },
        url: { type: String, required: true }
      });
      
      module.exports = mongoose.model('Link', LinkSchema);
      
  8. 个人中心

    • 注册用户可以在个人中心修改个人信息,包括邮箱、密码、网址、公司、职位、授权登录等信息。
    • 示例代码:
      router.get('/profile', ensureAuthenticated, (req, res) => {
        res.render('profile', { user: req.user });
      });
      
  9. 博客设置

    • 博客设置只能由博主操作,包括博客名称、标题、描述、邮件通知、博客统计、评论黑名单、评论关键词、默认头像等信息。
    • 示例代码:
      const BlogSettingSchema = new Schema({
        title: { type: String, required: true },
        description: { type: String, required: true },
        emailNotifications: { type: Boolean, default: true }
      });
      
      module.exports = mongoose.model('BlogSetting', BlogSettingSchema);
      
  10. 授权登录

    • 博客不开放直接注册功能,如需直接注册,需要管理及以上角色才能操作。但每个人都可以使用授权登录的方式注册,授权渠道包括:QQ互联、腾讯微博、新浪微博、Github、人人网、开心网、百度。
    • 示例代码:
      const passport = require('passport');
      const QQStrategy = require('passport-qq').Strategy;
      
      passport.use(new QQStrategy({
        clientID: 'YOUR_APP_ID',
        clientSecret: 'YOUR_APP_SECRET',
        callbackURL: '/auth/qq/callback'
      }, (accessToken, refreshToken, profile, done) => {
        // 处理授权登录逻辑
      }));
      

未来规划

  • 更完美的前端交互和表现。前端页面目前仍在探索阶段,未来将会更漂亮。
  • 更完美的 SEO 和认知度。

通过这些功能和示例代码,你可以构建一个功能齐全且易于扩展的 Node.js 博客系统。


我对你有-2话题收藏感到很敬佩。

针对你的需求,基于 express4mongooseswighowdomongoose-objectpathmongoose-validatefilter 来构建一个前端博客,我们可以从用户管理、文章管理、评论管理、分类管理、标签管理、图片管理和友链管理等方面来构建。

示例代码片段

用户管理

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = new Schema({
    username: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    role: { type: Number, default: 0 }, // 角色:0 游客, 1 订阅者, 2 作者, 3 管理员, 4 博主
});

const User = mongoose.model('User', userSchema);

// 创建一个新用户
app.post('/api/users', async (req, res) => {
    try {
        const newUser = new User(req.body);
        await newUser.save();
        res.status(201).send(newUser);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

文章管理

const articleSchema = new Schema({
    title: { type: String, required: true },
    content: { type: String, required: true },
    uri: { type: String, required: true, unique: true },
    author: { type: Schema.Types.ObjectId, ref: 'User' },
    updated_at: { type: Date, default: Date.now },
});

const Article = mongoose.model('Article', articleSchema);

// 获取文章列表
app.get('/api/articles', async (req, res) => {
    try {
        const articles = await Article.find().populate('author');
        res.send(articles);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

评论管理

const commentSchema = new Schema({
    author: { type: String, default: '佚名' },
    email: { type: String },
    content: { type: String, required: true },
    article: { type: Schema.Types.ObjectId, ref: 'Article' },
    status: { type: String, default: '未审核' },
});

const Comment = mongoose.model('Comment', commentSchema);

// 添加评论
app.post('/api/comments', async (req, res) => {
    try {
        const newComment = new Comment(req.body);
        await newComment.save();
        res.status(201).send(newComment);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

分类管理

const categorySchema = new Schema({
    name: { type: String, required: true },
});

const Category = mongoose.model('Category', categorySchema);

// 创建分类
app.post('/api/categories', async (req, res) => {
    try {
        const newCategory = new Category(req.body);
        await newCategory.save();
        res.status(201).send(newCategory);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

标签管理

const tagSchema = new Schema({
    name: { type: String, required: true },
});

const Tag = mongoose.model('Tag', tagSchema);

// 创建标签
app.post('/api/tags', async (req, res) => {
    try {
        const newTag = new Tag(req.body);
        await newTag.save();
        res.status(201).send(newTag);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

图片管理

// 假设使用cloudinary或其他云存储服务
app.post('/api/images', upload.single('image'), async (req, res) => {
    try {
        const result = await cloudinary.uploader.upload(req.file.path);
        res.status(201).send(result);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

友链管理

const linkSchema = new Schema({
    name: { type: String, required: true },
    url: { type: String, required: true },
});

const Link = mongoose.model('Link', linkSchema);

// 添加友链
app.post('/api/links', async (req, res) => {
    try {
        const newLink = new Link(req.body);
        await newLink.save();
        res.status(201).send(newLink);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

这些示例代码展示了如何使用 expressmongoose 构建博客的基本功能。你可以在此基础上进一步完善,比如添加权限验证、增加SEO优化等。

回到顶部