jQuery Ajax发布未定义

I've been searched about this problem over stackoverflow, but couldn't get any answer that solves my problem.

This is even sample code in my node.js book. I don't know why this is not working and I want to sue author of this book. He gave me lot of stress.

1] Code in server.js

app.post('/products', function (request, response){

console.log(request.params);
var name = request.params.name;
var price = request.params.price;
var item = {
    name : name,
    price : price
};

items.push(item);

response.send({
    message : 'Data pushed',
    data : item
    });
});

2] Code in index.html

$('#post').click(function(){
     console.log($('#name').val());
     console.log($('#price').val());
     $.ajax({
        url:'/products',
        type:'post',
        dataType : 'text',
        data : {
          name : $('#name').val(),
          price : $('#price').val()
        },
        success : function(data){
          $('#output').val(data);
        }
    });
});

I can see correct data which I put in #name and #price in console of Chrome browser by console.log. But I can't see any data in server.js by console.log(request.params). console.log(request.params), console.log(request.params.id), console.log(reqeust.params.price) all returns 'undefined' Why does it return undefined? I tried lots of things I searched through internet, but nothing solved. What's the problem?

Did you used Expess in you server file?

var app = express();
app.use(express.bodyParser());

Or

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

In server js you need to use request.body instead of request.params like,

var name = request.body.name;
var price = request.body.price;

You are sending json from server so use dataType : 'json' like,

$.ajax({
    url:'/products',
    type:'post',
    dataType : 'json', // use json instead of text
    data : {
      name : $('#name').val(),
      price : $('#price').val()
    },
    success : function(data){ 
        console.log(data);// for name use data.data.name
        //$('#output').val(data);
    }
});