node.js - Get the request.body in NodeJs -
i have basic server:
var http = require("http"); var url = require("url"); var routing = require("./routingpath"); function start() { function onrequest(request, response) { var path = url.parse(request.url).pathname; var query = url.parse(request.url,true).query; routing.route(path.tolowercase(), query, function(recordset){ response.writehead(200, {"content-type": "application/json"}); if (recordset != null) { console.log(json.stringify(recordset, null, 4)); response.write(json.stringify(recordset, null, 4)); } response.end(); console.log(); }); } http.createserver(onrequest).listen(8888); console.log("server has started!"); }
i utilize "httprequester" post request, , add together attachment within in (small file) or utilize content textbox send info in it. in server request no access it's body. tried:
console.log(request.body);
but it's undefined.
i tried print request data, it's long , can't see request.
i tried other request because thought "httprequester" may not send me right request friend sent me client.
how can access body of request?
if need utilize default http module need read request body info stream:
if (request.method == 'post') { request.on('data', function(chunk) { console.log("received body data:"); console.log(chunk.tostring()); }); }
i recommend have @ expressjs, features routing , has various body parser's files/json etc.
node.js request
No comments:
Post a Comment