-1

I'm beginer programer and I have some troubles with server's response.

Basicly I'm using Express to make it work but it doesn't and returns me 'undefined' in request.body in client server file.

Here's my router code on server. This code is working fine and sending good response in postman:

child = childProcess.exec('javac code.java', function (err, output, outerr) {
if (err) {
    console.log(err.message);
    res.end(err.message);
}
else if (outerr) {
    res.end(outerr);
}
else if (output) {
    childProcess.exec('java code', function (err, output, outerr) {
        if (err) {
            res.end(err.message);
        }
        else if (outerr) {
            res.end(outerr);
        }
        else if (output) {
            res.end(output);
        }                                                      
    ...And the closing quotes

Here's other, client server. This server is making a request to the server above, with java code in body to execute it.

It's sending empty response and in console it's saying 'recived response: undefined'

router.post('/codeEditor', function (req, res) {
    var request = http.request({port: 8000, method: 'POST', path: '/java', headers:{'Content-type':'application/json'}}, function (response) {
        console.log('recived response: '+ response.body);
        res.end(response.body);
    });
    request.write(JSON.stringify({'sample':'sample'}));
    request.end();
    request.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

});

The request.write(JSON.stringify({'sample':'sample'})); is for test purposes, as it should be passing req.body object.

I've got installed body.parser and it looks like this:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var router = require('./routes');


app.use(bodyParser.json({type: 'application/json'}));
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.text({type: '*/*'}));

app.use('/', router);


app.listen(8000, function () {
    console.log('server listening on port 8000');
});

So I have no clue what is happening here Thanks for any help! :)

  • 1
    *I've got installed body.parser* - can you show **how** you've "installed" body.parser? because your code does not show you using body-parser in either a top-level generic nor a route specific way – Jaromanda X Jun 25 '17 at 12:01
  • I'll submit the code, when i get back home :) – Kuba Maliszewski Jun 25 '17 at 16:26

2 Answers2

1

I MADE IT

I don't know how it's possible but I simply changed the client server code from:

 var request = http.request({port: 8000, method: 'POST', path: '/java', headers:{'Content-type':'application/json'}}, function (response) {
    console.log('recived response: '+ response.body);
    res.end(response.body);
});

To:

 var request = http.request({port: 8000, method: 'POST', path: '/java', headers:{'Content-type':'text/plain'}}, function (response) {
        response.on('data', function (chunk) {
            res.end(chunk);
        });
    });

And that's it! So not a big change, but it made me reeaallyy happy today :))

-1

You are use res.end on the java execution server. Which ends the request without any data. If you intend to send data back, use res.send.

This question details about the difference between the two.

Shivam
  • 3,462
  • 1
  • 15
  • 20