1. 程式人生 > >node10---GET請求和POST請求的參數

node10---GET請求和POST請求的參數

gin ida host req div lis ble form 文件上傳

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);

node10---GET請求和POST請求的參數