MongoDB + Express + node + bootstrap build multi-person Blog

Source: Internet
Author: User

This blog explains how to build a multiplayer blog, which requires a certain amount of basic knowledge for thought-finishing and memo-writing.

The first step: Create a new folder blog, the structure is as follows:

    • The bin---executable binary, the final boot interface.
    • Models---Store files related to data and operational data.
    • Public---store static files.
    • Routes---store routing files.
    • Views---store view files.
    • App.js---program Master file.
    • Package.json---program related information.

Step Two: Install the dependent module

CNPM Install Express express-session Connect-mongo connect-flash ejs markdown multer Morgan Serve-favicon Debug Cookie-par Ser Body-parser MongoDB--save

The main functions of the module are as follows:

    • Express---Web framework
    • Express-session---Express session middleware for session processing
    • Connect-mongo---Used in conjunction with session to connect the session to the database
    • Connect-flash---Page hints middleware, based on session implementation
    • Ejs---template engine
    • Markdown---markdown text parsing middleware.
    • Multer---The form data that is used to process enctype= "Multipart/form-data".
    • Morgan---log middleware.
    • Serve-favicon---Processing Web site icon Middleware
    • Debug---debugging using
    • Cookie-parser---middleware for processing cookies
    • Body-parser---Middleware for processing post requests
    • MongoDB---Connection database

Step three: Add a configuration file

Add the Setting.js file in the home directory, configure the port number, database and other basic information, as follows:

Module.exports = {    'myblog',     'blog'  ,    'localhost',    27017};

Fourth step: function design and routing design

    • Register, login, log out
    • View articles, edit articles, post articles, delete articles
    • Comments (Messages)
    • Page notification: implemented using Connect-flash. Connect-flash is based on the session, the principle is simple: Set the initial value, req.session.flash={} by req.flash(name, value) setting the field and value under this object, by req.flash(name) getting the value under this object, and delete this field.

Fifth Step: Permission settings

Add the Middlewares folder to the home directory and add the file Check.js as follows:

Module.exports ={checklogin:function Checklogin (req, res, next) {if(!req.session.user) {Req.flash ('Error','not logged in'); returnRes.redirect ('/signin');  } next (); }, Checknotlogin:function Checknotlogin (req, res, next) {if(Req.session.user) {Req.flash ('Error','is logged in'); returnRes.redirect (' Back');//return to previous page} next (); }};

Sixth step: Routing Files

Set up routing based on features

MongoDB + Express + node + bootstrap build multi-person Blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.