博客
关于我
node学习之路:day6(express重写留言板)
阅读量:278 次
发布时间:2019-03-01

本文共 2486 字,大约阅读时间需要 8 分钟。

知识点

  • express 中使用模板引擎 express-art-template
  • 使用 body-parser 中间件解析 post 请求
  • express 开放文件夹可访问 app.use('/public/', express.static('./public/'))
  • express 重定向 res.redirect('/')

目录结构

  • app.js
  • node_modules
  • public
    • css
    • js
    • img
    • lib
  • view
    • index.html
    • post.html

核心代码

# express-art-template 依赖了 art-templatenpm install art-template express-art-template --save
// app.jsvar express = require('express')var bodyParser = require('body-parser')var app = express()app.use('/public/', express.static('./public/'))// 配置使用 art-template 模板引擎// 第一个参数,表示,当渲染以 .html 结尾的文件的时候,使用 art-template 模板引擎app.engine('html', require('express-art-template'))// 第一个参数,表示,当渲染以 .art 结尾的文件的时候,使用 art-template 模板引擎// app.engine('art', require('express-art-template'))// Express 为 Response 相应对象提供了一个方法:render// render 方法默认是不可以使用,但是如果配置了模板引擎就可以使用了// res.render('html模板名', {模板数据})// 第一个参数不能写路径,默认会去项目中的 views 目录查找该模板文件// 也就是说 Express 有一个约定:开发人员把所有的视图文件都放到 views 目录中// 如果想要修改默认的 views 目录,则可以// app.set('views', render函数的默认路径)// 配置 body-parser 中间件(插件,专门用来解析表单 POST 请求体)// parse application/x-www-form-urlencodedapp.use(bodyParser.urlencoded({    extended: false }))// parse application/jsonapp.use(bodyParser.json())var comments = [{       name: '张三',    message: '今天天气不错!',    dateTime: '2015-10-16'  },  {       name: '张三2',    message: '今天天气不错!',    dateTime: '2015-10-16'  },  {       name: '张三3',    message: '今天天气不错!',    dateTime: '2015-10-16'  },  {       name: '张三4',    message: '今天天气不错!',    dateTime: '2015-10-16'  },  {       name: '张三5',    message: '今天天气不错!',    dateTime: '2015-10-16'  }]app.get('/', function (req, res) {     res.render('index.html', {       comments: comments  })})app.get('/post', function (req, res) {     res.render('post.html')})// 当以 POST 请求 /post 的时候,执行指定的处理函数// 这样的话我们就可以利用不同的请求方法让一个请求路径使用多次app.post('/post', function (req, res) {     // 1. 获取表单 POST 请求体数据  // 2. 处理  // 3. 发送响应  // req.query 只能拿 get 请求参数  // console.log(req.query)    // 配置了body-parser才有req.body,否则没有  var comment = req.body  comment.dateTime = '2017-11-5 10:58:51'  comments.unshift(comment)  // res.send  // res.redirect  // 这些方法 Express 会自动结束响应  res.redirect('/')  // res.statusCode = 302  // res.setHeader('Location', '/') })// app.get('/pinglun', function (req, res) {   //   var comment = req.query//   comment.dateTime = '2017-11-5 10:58:51'//   comments.unshift(comment)//   res.redirect('/')//   // res.statusCode = 302//   // res.setHeader('Location', '/')// })app.listen(3000, function () {     console.log('running...')})

转载地址:http://cipo.baihongyu.com/

你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>