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