I have come across a weird processing issue with Express. I am able to retrieve the expected response via NodeJS when I am not using express.
Below is my code snippet which works with NodeJS
var http = require('http');
var url = require('url');
function start(route, handle){
function onRequest(request, response){
var postData = "";
var pathName = url.parse(request.url).pathname;
console.log('request obtained for '+pathName);
request.setEncoding('utf8');
request.addListener('data', function(chunk){
postData += chunk;
console.log(chunk);
});
request.addListener('end', function(){
**console.log(request.headers);**
route(handle, pathName, response, postData);
});
}
http.createServer(onRequest).listen(69);
console.log('server start on 69');
}
exports.start=start;
Part of request handler file, and the upload function is called when form data is submitted. The headers I am printing off are from the listener that I have for when the request ends.
function upload(response, postData){
console.log('Request handler upload');
var res = querystring.parse(postData).text;
console.log(res);
response.writeHead(200, {"Content-Type":"text/html"});
response.write('You sent a text: '+res);
console.log(postData);
response.end();
}
The curl call that I make to the upload function(here I am not uploading any files or images yet, so it is only text transfer for now)
curl -d "username=moderation&moderation.item[0].uid=4&moderation.item[0].action=approved" -H 'Content-Type: application/x-www-form-urlencoded' http://localhost:69/upload
The request headers from the curl call to the app running on nodejs
{ 'user-agent': 'curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3',
host: 'localhost:69',
accept: '*/*',
'content-type': 'application/x-www-urlencoded',
'content-length': '75' }
request to router for path/upload
Request handler upload
text=moderation&moderation.item[0].uid=4&moderation.item[0].action=approved
As your see the body is coming along as expected.
Now below is the code to my express app, which pretty much does the same thing.
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
/**custom stuff**/
app.post('/upload',function(req, res){
console.log(req.header('Content-Type'));
console.log(req.header('Host'));
console.log(req.header('User-Agent'));
console.log(req.body);
res.send("<h1> Hello the response is "+req.body.username);
});
/** end**/
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
And below is the curl call I made to this upload method.
curl -d "username=moderation&moderation.item[0].uid=4&moderation.item[0].action=approved" -H 'Content-Type: application/x-www-form-urlencoded' http://localhost:3000/upload
And the headers and the response below.
application/x-www-form-urlencoded
localhost:3000
curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
{ username: 'moderation',
'moderation.item': { '0].ui': '4', '0].actio': 'approved' } }
POST /upload 200 69ms - 32b
So the response body that is coming back is distorted. When I make a call to my nodejs app, I am getting
text=moderation&moderation.item[0].uid=4&moderation.item[0].action=approved
while for my express app, I am getting
{ username: 'moderation',
'moderation.item': { '0].ui': '4', '0].actio': 'approved' } }
I tried using connect.bodyParser() & also just express.urlencoded(), thinking that the bodyParser method has been screwing with my post data. I still think that it is the bodyParser but I am unable to find a way to get rid of this problem.
Sorry for the length of the post, but I wanted to provide all the data I could.
Any help is appreciated.
Thank you.