当我从客户端向运行express的node服务器发送JSON数据时,我遇到了问题。
这是一个演示我的问题的简单服务器:
var express = require('express');
var app = express();
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
app.use(express.logger());
});
app.listen(80);
app.post('/', function(req,res){
console.log(req.body);
console.log(req.body.number + 1);
});
该服务器只是将所有的POST数据记录到控制台。
如果我将以下内容粘贴到Chrome开发控制台中:
$.post('/', {number:1});
服务器会打印出:
This server simply logs all POST data to the console.
If I then paste the following into chrome's development console:
$.post('/', {number:1});
The server prints out:
{ number: '1' }
11
我该如何防止传递的数字被解释为字符串?这与我使用的bodyParser中间件有关吗?
非常感谢任何帮助!!