需要:nodejs,mysql
1.安装express,使用express创建项目框架
>express -e blog
创建好项目,后下载模块
>cd blog
>npm install
,安装需要的模块。
修改package.json中的dependencies内容,增加
{
mysql:'*',
node-mysql:'*',
connect-flash:'*'
}
再次加载:
>npm install
,加载好之后,修改app.js,将模板引擎改为html:
app.set('views', path.join(__dirname, 'views'));
app.engine('.html',ejs.__express);
app.set('view engine', 'html');
app.use(flash());
这样就可以在views目录下添加新的HTML页面了。
下面做一个登陆的流程。
login.html
<% include header.html %>
<form class="form-horizontal logform" id="log-form" role="form"action="/login" method="post" >
<%
if(true){
}
%>
<div class="form-group">
<label for="firstname" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-10">
<input type="text" class="form-control forminput" id="username" name="username"
placeholder="请输入用户名">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input type="password" class="form-control forminput" id="password" name="password"
placeholder="请输密码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">登录</button>
</div>
</div>
</form>
<% include footer.html %>
header.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title><%=title%></title>
<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="/stylesheets/style.css" />
</head>
<body>
<h1><%= title %></h1>
<hr>
<ul class="nav nav-pills nav-stacked" style="width:200px;position: absolute;">
<li role="presentation" class="active"><a href="/">首页</a></li>
<li role="presentation"><a href="/login">登录</a></li>
<li role="presentation"><a href="/reg">注册</a></li>
</ul>
footer.html
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/scripts/jquery.validate.min.js"></script>
<script>
$.extend($.validator.messages, {
required:"必填字段,不能为空",
minlength:"最少为{0}个字"
});
</script>
</body>
</html>
index.js
var User = require('./mysql-user');
module.exports=function(app){
app.get('/',function(req,res){
res.render('index',{title:'首页',
content:'这是首页'
});
});
app.get('/reg',function(req,res){
res.render('reg',{
title:'注册'
});
});
app.get('/login',function(req,res){
res.render('login',{
title:'用户登录',
user:'',
success:req.flash('success').toString(),
error:req.flash('error').toString()
});
});
app.post('/login',function(req,res){
var data=req.body;
if(data){
User.get(data.username,function(err,user){
if(err || !user){
res.flash('error',"用户不存在");
return res.redirect('/login');
}
if(user.password!=data.password){
res.flash('error',"密码错误");
return res.redirect('/login');
}
console.log("user:"+user);
req.session.user=user;
req.flash('success','登陆成功');
return res.redirect('/');
});
}
});
app.post('/reg',function(req,res){
var data=req.body;
console.log(data);
User.get(data.username,function(err,user){
});
});
};
mysql-user.js
var mysql=require('mysql');
var settings=require('../settings'),config=settings.mysql;
function User(user){
this.username=user.username;
this.password=user.password;
this.email =user.email;
}
var conn=mysql.createConnection(config);
conn.connect();
module.exports=User;
User.prototype.save=function(callback){
conn.query(' insert into t_user (username,password)values(\''+this.username+'\',\''+this.password+'\')',function(err,req){
if(err){
console.log(err);
callback(err);
}
callback(null,user);
});
}
User.get=function(name,callback){
conn.query(' select * from t_user where username=\''+name+'\'',function(err,rows){
if(err){
console.log(err);
//callback(err);
}
console.log("rows:"+rows);
if(rows!=null && rows!='' && rows!= undefined){
console.log('function come in here');
}
if(rows!=null && rows!='' && rows!= undefined){
callback(null,rows);
}else{
callback(err);
}
//callback(null,rows);
});
}
settings.js
module.exports={
cookieSecret:'blog',
db:'blog',
host:'localhost',
mysql:{
user:'root',
host:'localhost',
port:3306,
password:'123456',
database:'db_node'
}
}
安装supervisor模块,然后>supervisor app 启动
D:\workspace\HBuilder\blog>supervisor ap
Running node-supervisor with
program 'app'
--watch '.'
--extensions 'node,js'
--exec 'node'
Starting child process with 'node app'
Watching directory 'D:\workspace\HBuilde
Express server listening on port 3000
登陆localhost:3000测试。