How to pass object parameters to functions in JavaScript

My server.js is

// server.js - the outer server loop
var http = require('http')
 ,  php = require("./phpServer");

function start() {
    function onRequest(request, response) {
        php.phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);
        response.write('Ending');
        response.end();
    }
    http.createServer(onRequest).listen(80);
    console.log("Server started.");
}

exports.start = start;

That calls php.phpServer every request with response as the 3rd param.

phpServer contains.

//
//  phpServer.js - a generic server to serve static files and
//
var fs = require('fs')
 ,  pathfuncs = require('path')
 ,  url = require('url')
 ,  mimetypes = require('./mimetypes')

function phpServer(root, request, response) {
    // serve static or pass to php. 
    var data = url.parse(request.url);
    var ext = pathfuncs.extname(data.pathname);
    fs.stat(root+request.url, function(err, stat) {
        if (err || !stat.isFile()) { // error or not file.
            console.log('404');
            response.writeHead(404);
            response.write('Not Found');
            return;
        }
        // exists - serve.
        console.log("serve("+root+request.url+", mimetypes.mimetype("+ext+"))");
        response.writeHead(200, {'Content-Type': mimetypes.mimetype(ext)});
        response.write('Somethign to serve');
        // fs.createReadStream(root+request.url).pipe(response);
    });
}

exports.phpServer = phpServer

As I see it, response is an object and is passed by reference, therefore the response.write() here should write to the response.

It doesn't. Response here is NOT the same as response in onRequest, so nothing in phpServer is sent to the browser - not code nor content.

The console.logs come out and show what I would expect.

How can I get the object response passed so I can call write on it?

------------- added later -------------------

I've tried to apply answers given and code for server.is now

// server.js - the outer server loop
var http = require('http')
 ,  fs = require('fs')
 ,  pathfuncs = require('path')
 ,  url = require('url')
 ,  mimetypes = require('./mimetypes')

function phpServer(root, request, res) {
    // code adapted from page 118 of Smashing Node.js by Guillermo Rauch
    // res is response provided to onRequest. 
    var data = url.parse(request.url);
    var ext = pathfuncs.extname(data.pathname);
    res.write('Start reply');
    fs.stat(root+request.url, function(err,stat) {
        // define delayed callback - reponse in scope
        if (err || !stat.isFile()) { // error or not file.
            console.log('404');
            res.writeHead(404);
            res.write('Not Found');
            res.end
            return;
        };
        // exists so serve.
        console.log("serve("+root+request.url+", mimetypes.mimetype("+ext+"))");
        res.writeHead(200, {'Content-Type': mimetypes.mimetype(ext)});
        res.write('The file contents');
        res.end;
    }   // end callback,
    );  // end fs.stat call. 
} // end phpServer

function start() {
    function onRequest(request, response) {
        phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);
    }
    http.createServer(onRequest).listen(80);
    console.log("Server started.");
}

exports.start = start;

This does not reply at all - it times out. However the call to res.writeHead will either fail, if res is out of scope/does not exist/undefined, or succeed if re is the param passed in.

It succeeds, and is followed by write and end, so please - what have I got wrong.

If the file does not exist I get a start reply and then a timeout.

At the res.write('Start reply'); res is the response param, yet it isn't later in the fs.stat call-back.

Why not? Damn - this is frustrating.

The call to response.end should be moved from the onRequest function to phpServer. As it stands phpServer cannot write anything else since the stream has been closed.

function onRequest(request, response) {
    php.phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);

    // response.end(); // move this to phpServer 
}

As explained in the documentation for response.end

This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete.

Your problem is not with parameter passing, it's with basic asynchronous control flow. The stat() function does not do its work immediately. Its callback parameter is called when it's done. You basically cannot structure the code the way you've done it. Instead, your "phpServer" code will need to take a callback parameter of its own, and call it after it does its work.