zoukankan      html  css  js  c++  java
  • express get和post数据

    1.get-无需中间件
    req.query
    server.use('/',function(req,res){
    req.query;
    })

    2.post -需要body-parser,这里需要先执行
    server.use(bodyparser.urlencoded({}));这个解析完body才执行下一步,这里面有两个参数 extended(扩展模式)
    和limit(限制)

    server.use('/',function(req,res){
    req.body;
    })


    3.链式操作:

    server.use(function(req,res,next){});
    server.use('/'function(req,res,next){});
    server.use(function(req,res,next){});

    4.中间件:
    封装简单的(body-parser)中间件,在bodyparser.js文件下
    const querystring = require('querystring');
    module.exports= {
    aaa: function () {
    return function (req, res, next) {
    var str = '';
    req.on('data', function (data) {
    str += data;
    });
    req.on('end', function () {
    req.body = querystring.parse(str);
    next();
    })
    }
    }
    }
    调用自己写的中间件
    const bodyParser2 = require('./bodyparser');
    var server = express();
    server.use(bodyParser2.aaa());
    server.use('/',function(req,res){
    console.log(req.body);
    });

    目标很遥远,但是如果你拼了命努力,即使到最后没有达到目标,但是你会发现,在某种程度上你已经成功了。
  • 相关阅读:
    mysql5.7慢查询开启配置
    easyui的datagrid删除一条记录后更新出问题
    easyui跨iframe属性datagrid
    struts2笔记12-声明式异常
    struts2笔记11-OGNL
    struts2笔记10-值栈
    linux命令学习03-grep
    struts2笔记09-动态方法调用
    1、GIT简介
    玩转Python语言之4:奇技淫巧
  • 原文地址:https://www.cnblogs.com/Cavalary/p/9289156.html
Copyright © 2011-2022 走看看