monsto
monsto

Reputation: 1238

Learning Node.js... curious of scope

I'm reading an ebook called "The Node Beginner Book". It's basically 1 huge ~50 page exercise that covers a bunch of basics. I've been workin in python recently, and have php from a past life. So I figured it was right up my alley when this book said it was intended for people "experienced with at least one object-oriented language like Ruby, Python, PHP or Java, only little experience with JavaScript, and completely new to Node.js."

Here's the context:


index.js

var server =  require("./server");
var router =  require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {}
handle ["/"] = requestHandlers.start;
handle ["/start"] = requestHandlers.start;
handle ["/upload"] = requestHandlers.upload;

server.start(router.route, handle);

server.js

var http = require("http");
var url = require("url");

function start(route, handle) {
    function onRequest(request, response) {
        var pathname = url.parse(request.url).pathname;
        console.log("> Rcvd: " + pathname);

        route(handle, pathname, response);
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server has started");
}

exports.start = start;

router.js

function route(handle, pathname, response) {
    console.log("Routing: " + pathname);
    if (typeof handle[pathname] === 'function'){
        return handle[pathname](response);
    } else {
        console.log("ReqH ERR: No handler for " + pathname);
        response.writeHead(404, {"Content-Type": "text/plain"});
        response.write("404 Not Found");
        response.end();
    }
}

exports.route = route;

requestHandlers.js

var exec = require("child_process").exec;

function start(response) {
    console.log("Request handler 'start' was called.");
    var content = "empty";

    exec("ls -lah", function (error, stdout, stderr) {
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write(stdout);
        response.end();
    });

    return content;
}

function upload(response){
    console.log("ReqH: upload");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("From upload()");
    response.end();
}

exports.start = start;
exports.upload = upload;

And then you start the app via node index.js.

Alright I see that the "order of execution", or the path that the app takes, begins with including server.js and router.js, so I understand that basically the app 'lives' in index.js, running errands and going to work in the other modules.

Here's the question... server.js refers to the function route(), but doesn't explicitly require router.js which is the module that defines route().

Does index.js set the scope for the modules that it included? Since router.route is included in index, does server get access to indexes scope without having to explicitly know where route() came from?

Javascript is so loose that these 'lazy' kinda things are proving kinda challenging to wrap my head around.

Upvotes: 1

Views: 103

Answers (1)

deowk
deowk

Reputation: 4318

it would be good to read up about CommonJS -> http://requirejs.org/docs/commonjs.html

index.js

var server =  require("./server");
var router =  require("./router");

...
// here you are passing in the route method as the route param
server.start(router.route, handle);

so what it's actually doing is calling the start function in server.js and passign router.route as the parameter 'route'

server.js

   var http = require("http");
    var url = require("url");
    
    // here route is just the name of the param 
    function start(route, handle) {
        function onRequest(request, response) {
            var pathname = url.parse(request.url).pathname;
            console.log("> Rcvd: " + pathname);
    
            route(handle, pathname, response);
        }
    
        http.createServer(onRequest).listen(8888);
        console.log("Server has started");
    }
    
    exports.start = start;

here you are exporting the start function and thats why you can access it like this var server = require('./server').start(router.route, handle) in index.js

Upvotes: 3

Related Questions