form登录
2016-12-30 本文已影响0人
overisover
var config={
port:3000,
viewPath:'./views',
staticPath:'./static'
}
var koa=require('koa');
var views=require('co-views');
var staticServe=require('koa-static');
var router=require('koa-router')();
var logger=require('koa-logger');
var parse=require('co-body')
var render =views('./static',{
map:{'html':'swig'}
})
var app=koa();
router.get('/form',function*(){
this.body= yield render('form.html')
})
router.post('/login',function*(){
var user={
name:'aaa',
password:'123'
}
var body=yield parse.form(this);
if(body.username==user.name&& body.password==user.password){
this.body='success'
}else{
this.body='err'
}
})
app.use(logger())//查看加载的详细信息
app.use(staticServe('./static'));
app.use(router.routes());
app.listen(config.port)
console.log(config.port)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="lib/bootstrap-3.3.0/dist/css/bootstrap.css">
<script src='lib/jquery-2.2.3.js'></script>s
</head>
<body>
<form action="/login" method='post' enctype="application/x-www-form-urlencoded">
<div class="form-group">
<label for="">用户名:</label>
<input type="text" name="username" class="form-control">
</div>
<div class="form-group">
<label for="">密码:</label>
<input type="text" name="password" class="form-control">
</div>
<button class="btn btn-success">提交</button>
</form>
</body>
</html>