2015年7月18日 星期六

node.js post example, with callback

test as :
http://httpbin.org/

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////


//Load the request module
var request = require('request');

//Lets configure and request
request({
    url: 'http://httpbin.org/', //URL to hit
    qs: {from: 'blog example', time: +new Date()}, //Query string data
    method: 'POST',
    //Lets post the following key/values as form
    form: {
        field1: 'data',
        field2: 'data'
    }
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response.statusCode, body);
    }
});


//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////

app.post('/postpage', function(req, res) {
  res.send('respond with a resource'+req.body.username); 
});




沒有留言:

張貼留言