chirag jain chirag jain - 23 days ago 10
Node.js Question

Node reply with NaN, whenever I pass any variable, that hold String Value to console.log()?

Like, given below is code, which serve a from to user. But when submit data to the form, on the console it says, 'NaN'

var server = require('http').createServer();
var fs = require('fs');
var util = require('util');
server.on('request', function(req,res){
switch(req.method){
case 'POST':
var body ='';
req.on('data',function(data){
body=+data;
});
req.on('end',function(){
console.log(body);
});
break;

case 'GET':
console.log('GET request');
res.statusCode = 200;
res.setHeader('Content-Type' , 'text/html');
res.end('<html><body><form action="/" method="POST"><input type="text" name="username"><input type="submit" value="submit"></form></body></
}

}).listen(4000);

Answer

replace body=+data; with body+=data;

Comments