zoukankan      html  css  js  c++  java
  • 开发框架Express

    一、使用原因

    由于nodejs原生的http核心模块在某些方面不足以应对开发需求,所以就需要使用框架来加快开发效率,让代码更高度统一。在nodejs中有许多web开发框架,以下介绍Express的使用

    二、安装

    三、初步使用

    // 引入包
    var express=require('express');
    // 创建服务器应用程序
    var app=express();
    // 当服务器收到get请求(敲回车一定是get请求),执行回调处理函数
    app.get('/',function(req,res){
        res.send('hello express')
    });
    // 绑定端口号
    app.listen(3000,function(){
        console.log('app is running at port 3000')
    });

    四、基本路由

    ①get

    // 当服务器收到get请求(敲回车一定是get请求),执行回调处理函数
    app.get('/',function(req,res){
        res.send('hello express')
    });

    ②post

    // 当服务器收到post请求,执行回调处理函数
    app.post('/',function(req,res){
        res.send('Got a POST resquest')
    });

    五、静态服务

    app.use('/public/',express.static('./public/'));
    //方式1:url当以/public/开头的时候,去./public/目录中找对应的资源,推荐
    
    
    app.use('/a/public/',express.static('./public/'));
    //方式2:url必须是以/a/public/开头,去./public/目录中找对应的资源(起别名)
    
    app.use(express.static('./public/'));
    //方式3:省略第一个参数,则可以通过省略 /public/ 的方式去访问(跳级)

    六、在express中使用art-template模板引擎

    ①安装

    npm install --save art-template
    npm install --save express-art-template

    ②配置

    //引入express框架
    var express=require('express')
    var app=express()
    //第一个参数默认是art,代表的是.art后缀的文件,这里可以写成html
    //express-art-template是express来整合art-templatedao的,虽然不用加载art-template,但是也必须安装
    app.engine('html',require('express-art-template'))

    ③使用

    app.get('/',function(req,res){
        //express为response相应对象提供了一个方法render('html模板名',{模板数据}),默认是不可以使用的,配置模板引擎以后就可以使用
        //第一个参数不能写路径,express默认会去项目中的views目录找index.html(约定视图文件放在views目录)
        res.render('index.html',{
            title:'hello world' 
        })
    })

    ④修改默认的views视图渲染存储目录

    //注意:第一个参数views欠我不能写错
    app.set('views',目录路径)

    七、express中获取post请求体(使用第三方包body-parser)

    ①安装

    npm install --save body-parser

    ②配置

    //引入express和body-parser
    var express=require('express')
    var bodyParser=require('body-parser')
    var app=express()
    //配置body-parser,配置以后,在req请求对象上会多出一个属性:body,后面就可以直接通过req.body来获取post请求的数据
    app.use(bodyParser.urlencoded({extended:false}))
    app.use(bodyParser.json())

    ③使用

    app.use(function(req,res){
        res.setHeader('Content-Type','text/plain')
        res.write('you posted:
    '))
        //可以通过req.body来获取post请求数据
        //JSON.stringify(对象,参数2,参数3),
        //参数2为null或者未提供,则对象所有的属性都会被序列化;参数3指定缩进用的空白字符串,数字代表有多少的空格
        res.end(JSON.stringify(req.body,null,2)))
    })

    八、利用express框架重构留言板案例

    ①额外安装一些包

    ②修改表单提交为post请求

    ③修改app.js文件

    // 加载模块
    var express=require('express');
    var bodyParse=require('body-parser');
    // 开放public目录
    var app=express();
    app.use('/public/',express.static('./public/'));
    // 配置art-template模板引擎
    app.engine('html',require('express-art-template'));
    // 配置body-parser
    app.use(bodyParse.urlencoded({extended:false}));
    app.use(bodyParse.json());
    // 模拟首页留言列表数据
    var comments=[
        {name:"赵一",message:"你用什么编辑器?",datetime:"2018-1-1"},
        {name:"孙二",message:"今天天气真好",datetime:"2018-1-1"},
        {name:"张三",message:"飞流直下三千只",datetime:"2018-1-1"},
        {name:"李四",message:"哈哈哈哈哈",datetime:"2018-1-1"},
        {name:"王五",message:"楼上是傻逼",datetime:"2018-1-1"}
    ]
    //处理请求路径
    app.get('/',function(req,res){
        res.render('index.html',{
            comments:comments
        })
    });
    app.get('/post',function(req,res){
        res.render('post.html')
    });
    app.post('/say',function(req,res){
        var comment=req.body;
        comment.datetime='2018-5-5';
        comments.unshift(comment);
        res.redirect('/');
    });
    // 绑定端口
    app.listen(3000,function(){
        console.log('server is running...')
    });
  • 相关阅读:
    Go语言基础之包
    Go语言基础之结构体
    Go语言基础之指针
    Goland debug失败
    Geymotion 中安装 arm
    Linux 常用的目录及作用
    CentOS7使用YUM安装Adobe Flash Player
    XShell 连接 VirtualBox CentOS7
    机械键盘的修理方法是什么
    CentOS7网络配置
  • 原文地址:https://www.cnblogs.com/EricZLin/p/9295888.html
Copyright © 2011-2022 走看看