How do i write(print) to the document from request block in node.js

I wrote some server side code that should get live data from yahoo and then print it to the console and to the browser when i'm running the server, the problem is that i can't find a function that is printing to the document from the request block. This is my code:

var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
var util = require('util');

tempvar = null;
var server = http.createServer(function(req, res) {
    //writing the headers of our response
    res.writeHead(200, {
        'Content-Type': 'text/plain'
    });

    // Variable Deceleration 
    // TODO: move from the global scope
    var ticker = "IBM";
    var yUrl = "http://finance.yahoo.com/q/ks?s=" + ticker;
    var keyStr = new Array();
    testTemp = null;

    //
    // The main call to fetch the data, parse it and work on it.    
    //
    request(yUrl, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var $ = cheerio.load(body);


            // the keys - We get them from a certain class attribute
            var span = $('.time_rtq_ticker>span');
            stockValue = $(span).text();
            console.log("Stock  - " + ticker + " --> text " + stockValue);
            //res.write("Stock  - " + ticker + " --> text " + stockValue);
            testTemp = stockValue;
// 

-- end of request --
        res.write("Stock value for: " + ticker + " is --> " + testTemp + "\n");
        res.write("12333\n");
        res.write('something\n');

        //printing out back to the client the last line
        res.end('end of demo');

## Heading ##
        }

    }); 

});

server.listen(1400, '127.0.0.1');

This is the error that i got in the console Files\node.js\node_modules\YfTemp.js:49 >>; SyntaxError: Unexpected end of input at Module._compile at Object.Module._extensions..js at Module.load at Function.Modul._load at Function.Module.runMain at strartup at node.js:906:3

Request works asynchronously. You need to put the printing part of your script inside the request callback block. Otherwise, ticker is not defined yet when the printing lines are reached.

request(yUrl, function(error, response, body) {
     if (!error && response.statusCode == 200) {
        var $ = cheerio.load(body);


        // the keys - We get them from a certain class attribute
        var span = $('.time_rtq_ticker>span');
        stockValue = $(span).text();
        console.log("Stock  - " + ticker + " --> text " + stockValue);
        //res.write("Stock  - " + ticker + " --> text " + stockValue);
        testTemp = stockValue;

        // -- end of request --
        res.write("Stock value for: " + ticker + " is --> " + testTemp + "\n");
        res.write("12333\n");
        res.write('something\n');

        //printing out back to the client the last line
        res.end('end of demo');

     }


});