//art-template for express
/*
npm install --save art-template
npm install --save express-art-template
*/
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
/*
配置使用 art-template 模板引擎,第一个参数表示,当渲染以 .art 结尾的文件的时候(也可以修改为其他后缀,如: .html ), 使用
art-template 模板引擎, express-art-template 是专门用来在 Express 中把art-template 整合到Express 中,原因在于 express-art-template 依赖了 art-template
*/
// app.engine('art',require('express-art-template'));
app.engine('.html',require('express-art-template'));
//如果要修改默认的 views 视图渲染存储目录,可以: app.set('views',目录路径);
app.use('/Node/NBook/',express.static('./views'));
//配置body-parser(中间件),专门用来解析post的请求体
//parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended : false }));
//parse application / json
app.use(bodyParser.json());
function GetNowFormatDate(){
var MyDate = new Date();
var year = MyDate.getFullYear();
var month = MyDate.getMonth();
var day = MyDate.getDate();
var h = MyDate.getHours(); // 0-23
var m = MyDate.getMinutes();
if ( m < 10 ) m = '0'+ m ;
var s = MyDate.getSeconds();
if( s < 10 ) s = '0' + s;
var NowTime = year + '/' + month + '/' + day + '/' + h + ':' + m;
return NowTime;
}
var contents = [
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
},
{
name: 'Vodka',
passage: '迄今为止的人生都大写着失败,但不妨碍我继续向前!',
dateTime: GetNowFormatDate()
}
];
/*
Express 为response 相应对象提供了一个方法: render , 配置了模板引擎就可以使用,第一个参数不能写路径,
默认会去项目中的 views 目录查找该模板文件,Express有个约定:开发人员会把视图文件都放进 views 目录中
-res.render('html模板名',{模板数据});
*/
app.get('/404',function(request,response){
response.render('404.html');
});
app.get('/',function(request,response){
//参数一:需要渲染的页面 , 参数二: 用于渲染的参数
response.render('index.html',{
content: contents
});
})
app.post('/form',function(request,response){
//1.获取表单 POST 请求体数据 2.处理 3.发送响应
//与 get方法的不同, request.query 只能获取get 的参数, post 不能使用
//在 Express 中没有内置获取表单 POST 请求体的 API ,需要引入第三方包: body-parser, 安装: npm install --save body-parser
/*
var express = require('express')
//引包
var bodyParser = require('body-parser');
var app = express();
//配置 body-parser ,只要加入这个配置,则在 request 请求对象上会多出一个属性: body ,则可以通过request.body 来获取表单 post 请求体数据
//parse application / x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/ json
app.use(bodyParser.json());
app.use(function( request, response ){
response.setHeader('Content-Type','text/plain');
response.write('My posted: \n');
//可以通过 request.body 来获取表单post 的请求体数据了
response.end(JSON.stringify(request.body , null , 2));
});
2.关于 response.send(); response.redirect(); 这些方法 Express 会自动结束响应
*/
response.send(request.body);
});
app.get('/post',function(request,response){
response.render('post.html');
});
//利用url.parse() 将url路径转换为便于操作的对象,第二个参数true 表示将查询(query 属性) 字符串转为一个对象
/*
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: '127.0.0.1:5000',
port: '5000',
hostname: '127.0.0.1',
hash: null,
search: '?Name=Vodka&comment=%E9%9D%9E%E5%B8%B8%E7%A5%9E%E5%A5%87',
query: [Object: null prototype] { Name: 'Vodka', comment: '非常神奇' },
pathname: '/form',
path: '/form?Name=Vodka&comment=%E9%9D%9E%E5%B8%B8%E7%A5%9E%E5%A5%87',
href: 'http://127.0.0.1:5000/form?Name=Vodka&comment=%E9%9D%9E%E5%B8%B8%E7%A5%9E%E5%A5%87'
}
*/
app.get('/form',function(request,response){
var comment = {};
comment.name = request.query.Name;
comment.passage = request.query.comment;
comment.dateTime = GetNowFormatDate();
contents.unshift (comment);
//重定向
response.redirect('/');
})
app.listen(5000,function(){
console.log('Server running...');
});