zoukankan      html  css  js  c++  java
  • Node.js常用express方法

    Node.js 手册查询-Express 方法

    1、send方法

           send 方法向浏览器发送一个响应信息,并可以智能处理不同类型的数据 send方法在输出响应时会自动进行一些设置,比如HEAD信息、HTTP缓存支持等等 类型可以是: String, Array, Object, Number. 当参数为一个String时,Content-Type默认设置为"text/html" 当参数为Array或Object时,Express会返回一个JSON 当参数为一个NumberExpress会帮你设置一个响应体,比如:200

    2、获取参数的常用方法

    (1)、req.body

    (2)、req.query

    (3)、req.params

    (一)、req.body例子

      body不是nodejs默认提供的,你需要载入body-parser中间件才可以使用req.body,这个方法通常用来解析POST请求中的数据

    <form action='/test' method='post'> 
        <input type='text' name='name' value='lmw'> 
        <input type='text' name='tel' value='1234567'> 
        <input type='submit' value='Submit'> 
    </form>
    app.post('/test', function(req, res) {
        console.log(req.body.name);
        console.log(req.body.tel);
    });

    (二)、req.query例子

      有nodejs默认提供,无需载入中间件,这个方法通常用来解析get请求中的数据

    GET /test?name=lmw&tel=123456789
    
    app.get('/test', function(req, res) {
        console.log(req.query.name);
        console.log(req.query.tel);
    });


    (三)、req.query和req.body同时使有

    <form action='/test?id=1' method='post'> 
        <input type='text' name='name' value='lmw'> 
        <input type='text' name='tel' value='123456789'> 
        <input type='submit' value='Submit'> 
    </form>
    app.post('/test', function(req, res) {
        console.log(req.query.id);
        console.log(req.body.name);
        console.log(req.body.tel);
    });

    (四)、req.params

    另一种方法传递参数给服务器,但是这不算是传统标准规范的做法,是属于 HTTP Routing 的延伸应用

    GET /test/lmw/123456789
    
    app.get('/test/:name/:tel', function(req, res) {
        console.log(req.params.name);
        console.log(req.params.tel);
    });

     

  • 相关阅读:
    [ 打败 IE 的葵花宝典 ] IE6中css常见BUG全集及解决方案
    js获取URl传递的参数
    几种C#框架提供的数据结构对单值查找的效率比较
    C#Hashtable与Dictionary性能
    Sql server 使用存储过程分页显示
    arrayList使用 与 foreach 使用
    LeetCode 047. 全排列 II DFS
    LeetCode 046. 全排列 dfs 和 dfs_swap
    LeetCode 048. 旋转图像
    LeetCode 040. 组合总和 II 非SET去重
  • 原文地址:https://www.cnblogs.com/haonanZhang/p/8305932.html
Copyright © 2011-2022 走看看