獲得get請求參數
app.get('/index',function (req,res) { var params = req.query })
獲得post請求參數,post發送的參數是在請求體中的,Express沒有提供獲取表單post請求體的api,我們需要使用到第三方包
cnpm install body-parser
在項目中引入
var bodyParser = require('body-parser')
添加解析插件,它將解析所有傳入請求的主體。
var express = require('express') var bodyParser = require('body-parser') var app = express() // 創建application/json 解析器 var jsonParser = bodyParser.json() // 創建 application/x-www-form-urlencoded 解析器 var urlencodedParser = bodyParser.urlencoded({ extended: false })
//把body解析器專門添加到需要它們的路由中 app.post('/login', urlencodedParser, function (req, res) { res.send('welcome, ' + req.body.username) }) app.post('/api/users', jsonParser, function (req, res) { // create user in req.body })