Node.js TypeError: Cannot call method 'write' of undefined

This is the code in nodejs for to call the openweather API and print the result on the 127.0.0.7:8124 but do not understand why it does not work

var http = require('http');

function getData(city, res){
    var urlData = 'http://api.openweathermap.org/data/2.5/weather?q='+city;

    http.get(urlData, function(resi) {
        var body = '';

        resi.on('data', function(chunk) {
            body += chunk;
        });

        resi.on('end', function() {
            var dataResponse = JSON.parse(body)
            res.write(dataResponse);
        });
    }).on('error', function(e) {
          res.write("Got error: " + e);
    });
}

// create http server
http.createServer(function (req, res) {
    var query = require('url').parse(req.url).query;
    var app = require('querystring').parse(query).city;
    // content header
    res.writeHead(200, {'Content-Type': 'text/plain'});
    if(app){
        console.log("ad: "+getData(app));
    } else res.write("Use url:port?city=xxxx");

    res.end();
}).listen(8124);
console.log('Server running at 8124');

this is the error

overflow@overflow-1015cx:~/Scrivania/nodeweather$ node app.js 
Server running at 8124
ad: undefined

/home/overflow/Scrivania/nodeweather/app.js:15
        res.write(dataResponse);
            ^
TypeError: Cannot call method 'write' of undefined
    at IncomingMessage.<anonymous> (/home/overflow/Scrivania/nodeweather/app.js:15:13)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
overflow@overflow-1015cx:~/Scrivania/nodeweather$ 

Why can not I return the result?

You are not passing the response object into getData

I believe it should look like this, but I have not tested it.

 if(app){
        console.log("ad: "+getData(app,res));
    } else res.write("Use url:port?city=xxxx");\

If you read the error, its not telling you that you can't write, it's saying that you're trying to call write on a null object. If you trace the clues as to how res can be null, it should become clear.

Nodejs is async, res.end() is called before of res.write inside the http request.. so you need to use some "promise" tecnique or at least callbacks. However this code cannot work since you're trying to write a parsed json string, but the write method accepts only strings or buffer...moreover getData doesn't return nothing..so console.log("ad: "+getData(app,res,res.end)); prints an undefined variable.

maybe this code more fits your idea ( tested and working using "rome" ):

    var http = require('http');

function getData(city, res,callback){
    var urlData = 'http://api.openweathermap.org/data/2.5/weather?q='+city;

    http.get(urlData, function(resi) {
        var body = '';

        resi.on('data', function(chunk) {
            body += chunk;
        });

        resi.on('end', function() {
            body=JSON.stringify(JSON.parse(body), null, 4)
            res.write(body);
            callback(body);
        });
    }).on('error', function(e) {
          res.write("Got error: " + e);
          callback("error");
    });
}

// create http server
http.createServer(function (req, res) {
    var query = require('url').parse(req.url).query;
    var app = require('querystring').parse(query).city;
    // content header
    res.writeHead(200, {'Content-Type': 'text/plain'});
    if(app){
        getData(app,res,function (message) {
            console.log("ad:",message);
            res.end();
        });
    } else { 
        res.write("Use url:port?city=xxxx");
        res.end("done");
    }

}).listen(8124);
console.log('Server running at 8124');