javascript - communicate between a server and a client with node.js -


i have node.js server:-

// *********** server receives orders ************ //  // use features of http protocol. //  var http = require('http');  // initialize empty string. // var req = "";  // create server receive order request. // var server = http.createserver(function(req,res) {   res.writehead(200, {'content-type': 'text/plain'});   // when data received, success message displayed. //   res.on('data', function(data){         req += data; // received data appended. //        console.log("we have received request successfully.");   }); });  // error message displayed - error event. //    server.on('error', function(e){    console.log("there problem request:\n" + e.message);   });  // server listens @ following port , localhost (ip). // server.listen(8000, '127.0.0.1'); 

and have node.js client:-

var http = require("http");  var querystring = require("querystring"); var postorder = querystring.stringify({         'msg': 'hello world!' });  var options = {         hostname: '127.0.0.1',         port: 8000,         path:'/order',         method:'post',         headers:{            'content-type' :'application/x-www-form-urlencoded',            'content-length' : postorder.length         } };   var req = http.request(options, function(res) {   console.log('status: ' + res.statuscode);   console.log('headers: ' + json.stringify(res.headers));   res.setencoding('utf8');   res.on('data', function (chunk) {     console.log('body: ' + chunk);   }); });  req.on('error', function(e) {   console.log('problem request: ' + e.message); });  // write data request body req.write(postorder); req.end(); 

i trying figure out how can make client post order server , response server...either success message or error message...using command line.

currently run server on cmd line $ node server.js

and run client $ node client.js

but no responses.

i think have problems server: server must be:

http.createserver(function(req, res) {     if (req.method == 'get') {      } else if (req.method == 'post') {         var body = '';         req.on('data', function(data) {             body += data;         });         req.on('end', function() {          console.log("we have received request successfully.");         });     }     res.end("ok"); }) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -