学习node.js。参照《node.js开发指南》使用express框架。编写路由部分遇到问题,在书中并没有这方面介绍。在此记录解决方法。
所遇到的问题是:如果项目越写越大,将会导致index部分的路由管理变得复杂起来,而且不好进行模块区分。解决如下,index部分引入其余部分路由模块,这里分为user和blog模块。具体原理也还不清楚,刚接触,以后在慢慢理解
index部分
var express = require('express');
var users=require('./route_users.js');
var blogs=require('./route_blogs.js');
module.exports = function(app){
app.get("/", function (req, res) {
res.redirect('/bloglist');
});
users(app);
blogs(app);
}
user模块
var express = require('express');
var crypto=require('crypto');
var router = express.Router();
var User=require('../sql_user/sql_user.js');
module.exports = function(app){
app.get("/reg",function(req,res){
res.render('users/reg', {title: 'NodeBlog-用户注册'});
});
app.get("/login",function(req,res){
res.render('users/login',{title:'NodeBlog-用户登录'});
});
app.post("/login",function(req,res){
var userName=req.body.username;
var password=req.body.password;
//是否存在验证
var md5 = crypto.createHash('md5');
password = md5.update(password).digest('base64');
User.login(userName,password,function(err,date){
if(err){
throw err;
req.flash('error','登录失败,数据异常!')
}else{
if(date!=''&&date[0].username==userName){
var newUser = new User({
name: userName,
password: password,
phoneNumber:date[0].phoneNumber,
});
req.flash('info','登录成功');
req.session.user=newUser;
return res.redirect('/bloglist');
}else{
req.flash('error','登录失败,账号或密码错误!');
return res.redirect('/login');
}
}
});
});
app.get('/logout',function(req,res){
req.session.user=null;
req.flash('info','注销成功');
res.redirect('/');
});
app.post('/reg',function(req,res){
//console.log(req);
var userName=req.body.username;
var password=req.body.password;
//是否存在验证
var md5 = crypto.createHash('md5');
password = md5.update(password).digest('base64');
var newUser = new User({
name: userName,
password: password,
phoneNumber:req.body.phoneNumber,
});
User.save(newUser,function(err){
if(err){
throw err;
req.flash('error','注册失败');
res.redirect('/reg');
}else{
req.session.user=newUser;
req.flash('info','注册成功');
res.redirect('/');
}
});
});
app.get('/addCheck',function(req,res){
User.addCheck(req.query.username,function(err,date){
if(err){
throw err;
}else {
res.json({"number":date[0].number});
}
});
});
}
blog模块
var express = require('express');
var crypto=require('crypto');
var router = express.Router();
var Blog=require('../sql_blog/sql_blog.js');
module.exports = function(app){
app.get("/bloglist",function(req,res){
var type=1,offset=0,rows=20,flag=false,seq=1;
if(req.query.type){
flag=true;
type=req.query.type;
offset=req.query.offset;
rows=req.query.row;
seq=req.query.seq;
}
Blog.getList(type,seq,offset,rows,function(err,data){
if(err){
throw err;
}else{
if(flag){
res.json({"blogs":data});
}else{
res.render('blogs/bloglist',{
title:'NodeBlog',
user:req.session.user,
blogs:data,
});
}
}
});
});
app.get("/blog/:title/:id",function(req,res){
//查询详情
Blog.getBlogIngo(req.params.id,function(err,data){
if(err) {
throw err;
}else{
res.render('blogs/bloginfo',{
title:req.params.title,
datas:data[0]
});
}
});
});
}