zoukankan      html  css  js  c++  java
  • node10---GET请求和POST请求的参数

    GET请求的参数在URL中,在原生Node中,需要使用url模块来识别参数字符串。在Express中,不需要使用url模块了。可以直接使用req.query对象。
    ● POST请求在express中不能直接获得,必须使用body-parser模块。使用后,将可以用req.body得到参数。但是如果表单中含有文件上传,那么还是需要使用formidable模块。
    
    Node中全是回调函数,所以我们自己封装的函数,里面如果有异步的方法,比如I/O,那么就要用回调函数的方法封装。
    错误:
    1res.reder("index",{
    2    "name" : student.getDetailById(234234).name
    3});
    4
    5
    正确:
    6
    7student.getDetailByXueHao(234234,function(detail){
    8    res.render("index",{
    9        "name" : detail.name
    10    })
    11});
    12
    1

    12.js

    /**
     * Created by Danny on 2015/9/22 14:37.
     */
    var express = require("express");
    
    var app = express();
    
    app.get("/",function(req,res){
        console.log(req.query);//识别参数字符串
        res.send();
    });
    
    app.listen(3000);

    13.js

    /**
     * Created by Danny on 2015/9/22 14:37.
     */
    var express = require("express");
    var bodyParser = require('body-parser')
    
    var app = express();
    
    //模板引擎
    app.set("view engine","ejs");
    
    app.get("/",function(req,res){//http://localhost:3000/
         res.render("form");
    });
    
    //bodyParser API,使用中间件
    app.use(bodyParser.urlencoded({ extended: false }))
    
    //post请求
    app.post("/",function(req,res){
        console.log(req.body);//req.body得到参数,{ name: 'ssss', age: 'vdvdvdv' }
    });
    
    app.listen(3000);
  • 相关阅读:
    不能选中EXCEL单元格直接复制内容到数据库
    trim c# .net
    Postion and AlignmentPoint
    format详解
    range()函数详解
    蓝桥杯,查找整数,python
    蓝桥杯,杨辉三角形,Python
    蓝桥杯,回文数,Python
    微信小程序页面间的数据传递和数据共享
    蓝桥杯,特殊回文数,Python
  • 原文地址:https://www.cnblogs.com/yaowen/p/7039991.html
Copyright © 2011-2022 走看看