Node.js learning (3) - with the express rewrite guestbook

installation
cnpm i -S express
cnpm i -S express-art-template
cnpm i -S art-template
 
var express = require('express')
 
was app = Out ()
 
var comments = [
{
name:'zj',
message:'hello',
datetime: '2019-08-14'
},
{
name:'zky',
message:'hello world',
datetime: '2019-08-13'
},
]
// app.engine () Configuration express-art-template template
app.engine('html', require('express-art-template'));
// 开放静态资源
app.use('/public/',express.static('./public'))
 
app.get('/',function(req,res){
// res对象本身是没有render方法的,当配置了express-art-template才会给其添加此方法
res.render('index.html',{
comments:comments
})
})
 
app.get('/post',function(req,res){
res.render('post.html',)
})
 
app.get('/pinglun',function(req,res){
var common = req.query;
common.dateTime = '2019-08-15';
comments.unshift(common);
res.redirect('/');
})
 
// app.post("/pinglun",(req,res)=>{
// //post方法获取到了请求参数
// // console.log(req.body);
// //req.body.dateTime 添加一个键值对
// req.body.dateTime="2015-10-16";
// comments.unshift(req.body);
// //重定向 参数是重定向的路径
// res.redirect("/");
// });
 
app.listen(3000,function(){
console.log('running')
})

Guess you like

Origin www.cnblogs.com/monica4/p/11360003.html