美文网首页
8.Express框架介绍与使用

8.Express框架介绍与使用

作者: 璎珞纨澜 | 来源:发表于2019-04-02 19:35 被阅读0次

    Express 的介绍与安装

    原生的 http 在某些方面表现不足以应对我们的开发需求,所以我们就需要使用框架来加快我们的开发效率,框架的目的就是提高效率,让我们的代码高度统一。
    在 node 中,有很多 Web 开发框架,比如express。

    • 安装


      Express 的安装
    • 使用
    var express = require('express')
    // 创建服务器应用,也就是原来的 http.createServer()
    var app = express()
    app.get('/', function (req. res){
      res.send('Hello World!')
    )}
    
    app.listen(3000, function () {
      console.log('express app is running ...')
    })
    

    基本路由

    路由器

    • 请求方法
    • 请求路径
    • 请求处理函数

    get 请求:

    // 当你以 GET 方法请求 / 的时候,执行对应的处理函数
    app.get('/', function (req, res) {
      res.send('Hello World!')
    }
    

    post 请求:

    // 当你以 POST 方法请求 / 的时候,执行对应的处理函数
    app.post('/', function(req, res) {
      res.send('Got a POST request')
    })
    

    express 中的静态服务

    //  /public 资源
    app.use(express.static('public'))
    // 当省略第一个参数的时候,则可以通过 省略 /public 的方式来访问
    // 这种方式的好处就是可以省略 /public/
    
    //  /files 资源
    app.use(express.static('files'))
    
    //  /public/xxx
    app.use('/public', express.static('public'))
    // 当以 /public 开头的时候,去 ./public/ 目录中找找对应的资源
    // 这种方式更容易辨识,推荐这种方式
    
    //  /static/xxx
    app.use('/static', express.static('public'))
    // 可以用 /static 访问 /public 目录中的资源
    
    // 静态函数的路径相对于 node 启动的路径
    // 如果从另一个目录运行 Express 应用程序,则使用要服务的绝对路径更安全
    app.use('/static', express.static(path.join(__dirname, 'public')))
    

    详细规则参见 express 官网:http://expressjs.com/en/starter/static-files.html

    在Express中配置使用 art-template 模板引擎:

    • 安装
      express-art-template 是专门用来把 art-template 整合到 Express 中的一个模块,这里必须安装 art-template 的原因是 express-art-template 依赖了 art-template。
      npm install --save art-template express-art-template

    • 配置
      配置使用 art-template 模板引擎,第一个参数,表示当渲染以 .art 结尾的文件的时候,使用 art-template 模板引擎。也可以改为html, 渲染的文件要改为 html 文件。

    app.engine('art', require('express-art-template'))
    
    • 使用
    app.get('/', function (req, res) {
    // express 默认会去 views 目录中找 index.html
      res.render('index.html', {
        title:'hello world'
      })
    })
    

    如果希望修改默认的 views 视图渲染存储目录,可以:

    app.set('views', 目录路径)
    

    express 写留言本案例

    1. 在 express 中获取表单 GET 请求参数

    Express 内置了一个 API,可以直接通过 req.query 来获取。

    2. 在 express 中获取表单 POST 请求体数据

    在 express 中没有内置的获取表单 POST 请求体的 API, 这里我们需要使用一个第三方包:body-parserbody-parser官方使用说明

    • 安装
      npm install --save body-parser
    • 配置
      只要加入这个配置,则在 req 请求对象上就会多出来一个属性:body。也就是说你可以直接通过 req.body 来获取表单 POST 请求体数据了。
    var express = require('express')
    var bodyParser = require('body-parser')
    
    var app = express()
    
    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: false }))
    // parse application/json
    app.use(bodyParser.json())
    
    • 使用
      通过 req.body 用来获取 POST 请求体数据。
    app.use(function (req, res) {
      res.setHeader('Content-Type', 'text/plain')
      res.write('you posted:\n')
      res.end(JSON.stringify(req.body, null, 2))
    })
    

    留言本案例代码:

    var express = require('express')
    var bodyParser = require('body-parser')
    var app = express()
    
    app.use('/public',express.static('public'))
    
    app.engine('html', require('express-art-template'))
    
    //配置 body-parser 中间件(专门用来解析表单 POST 请求体)
    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: false }))
    
    // parse application/json
    app.use(bodyParser.json())
    
    var comments = []
    
    app.get('/', function(req, res) {
        res.render('index.html', {
            comments: comments
        })
    })
    
    app.get('/post', function(req, res) {
        res.render('post.html')
    })
    // GET 请求
    // app.get('/comments', function(req, res) {
    //  var comment = req.query
    //  comment.dateTime = '2019-04-02 15:45:00'
    //  comments.unshift(comment)
    //  res.redirect('/')
    // })
    // POST 请求
    app.post('/post', function(req, res) {
        console.log(req.body)
        var comment = req.body
        comment.dateTime = '2019-04-02 15:45:00'
        comments.unshift(comment)
        res.redirect('/')
    })
    
    app.listen(3000, function () {
        console.log('running...')
    })
    

    相关文章

      网友评论

          本文标题:8.Express框架介绍与使用

          本文链接:https://www.haomeiwen.com/subject/gtgkbqtx.html