1. GET—query
http://localhost:8080/?age=12
app.get('/', function (req, res) {
var params = req.query
res.send(params)
})
//{"age": "12"}
2. GET—params
http://localhost:8080/13
app.get('/:id', function (req, res) {
var params = req.params
res.send(params)
})
// {"id": "13"}
3. POST
//index.html
var url = 'http://localhost:8080/post'
$.ajax({
url: url,
data: {
params1: 'post_p1',
params2: 'post_p2'
},
dataType: 'json',
type: 'POST',
success: function (data) {
console.log(data)
}
})
//node app.js
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.post('/post', function (req, res) {
var params = req.body
console.log(params)
//{ params1: 'post_p1', params2: 'post_p2' }
})