Home  >  Article  >  Web Front-end  >  Node.js blog example (4) Implementing user pages and article pages_html/css_WEB-ITnose

Node.js blog example (4) Implementing user pages and article pages_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:59:03849browse

Chapter 4 of the original tutorial https://github.com/nswbmw/N-blog/wiki/_pages. Due to version reasons, etc., it can be implemented with slight changes based on the original tutorial.

Now, let’s add user pages and article pages to the blog.
The so-called user page means that when you click on a username link, it will jump to: domain name/u/username and list all the articles of the user.
Similarly, the article page means that when you click on the title of an article, it will jump to: domain name/u/user name/time/article name and enter the page of the article.

post.js:

Modify Post.get to Post.getAll, and modify Post.get in index.js to Post.getAll. Add the following code at the end of post.js:

//获取一篇文章Post.getOne = function(name, day, title, callback) {  //打开数据库	mongodb.open(function (err, db) {		if (err) {			return callback(err);		}		//读取 posts 集合		db.collection('posts', function (err, collection) {			if (err) {				mongodb.close();				return callback(err);			}			//根据用户名、发表日期及文章名进行查询			collection.findOne({				"name": name,				"time.day": day,				"title": title			}, function (err, doc) {				mongodb.close();				if (err) {									return callback(err);				}				console.log("333");				console.log(doc);				//解析 markdown 为 html				doc.post = markdown.toHTML(doc.post);				console.log("444");				callback(null, doc);//返回查询的一篇文章				console.log("555");			});		});	});};
Post.getAll: Get all articles of a person (pass in parameter name) or get everyone's articles (do not pass in parameter).
Post.getOne: Get an article accurately based on user name, publication date and article name.
index.js: Add the following code:

app.get('/u/:name', function (req, res) {		//检查用户是否存在		User.get(req.params.name, function (err, user) {			if (!user) {				req.flash('error', '用户不存在!'); 				return res.redirect('/');//用户不存在则跳转到主页			}			//查询并返回该用户的所有文章			Post.getAll(user.name, function (err, posts) {				if (err) {					req.flash('error', err); 					return res.redirect('/');				} 				res.render('user', {					title: user.name,					posts: posts,					user : req.session.user,					success : req.flash('success').toString(),					error : req.flash('error').toString()				});			});		}); 	});		app.get('/u/:name/:day/:title', function (req, res) {		Post.getOne(req.params.name, req.params.day, req.params.title, function (err, post) {			if (err) {				req.flash('error', err); 				return res.redirect('/');			}			res.render('article', {				title: req.params.title,				post: post,				user: req.session.user,				success: req.flash('success').toString(),				error: req.flash('error').toString()			});		});	});
Create a new user.ejs in the blog/views/ folder, add the following code, and also add index.ejs Modify to the following code:
<%- include header %><% posts.forEach(function (post, index) { %>  <p><h2><a href="/u/<%= post.name %>/<%= post.time.day %>/<%= post.title %>"><%= post.title %></a></h2></p>  <p class="info">    作者:<a href="/u/<%= post.name %>"><%= post.name %></a> |     日期:<%= post.time.minute %>  </p>  <p><%- post.post %></p><% }) %><%- include footer %>

Create a new article.ejs in the blog/views/ folder and add the following code:

<%- include header %><p class="info">  作者:<a href="/u/<%= post.name %>"><%= post.name %></a> |   日期:<%= post.time.minute %></p><p><%- post.post %></p><%- include footer %>

Now, we have added user page and article page to the blog. To achieve the effect:

Click on the article title:


Click on the author:


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn