We first start at index.js. Basically it first requires the module files ‘server’ and ‘router’ and recognizes the interface and definition implemented in those files. It puts the function definitions and variables into global var server an router for us to use later.
Hence that’s why you will see the console.logs of those files
start of index.js
start of server.js
end of server.js
start of router.js
end of router.js
…then after requiring those files, we get display the log:
‘server variable running function start with parameter router’
Our variables to use are server and router.
in index.js
1 2 3 4 5 6 7 |
//get the module server, which is the server.js we just wrote //put it into the variable server var server = require("./server"); //get the router module, which is router.js we just wrote //put it into the variable router var router = require("./router"); |
We now use the server variable to call function start.
The whole point of router.js is to provide the function route, which we pass into the server.start for it to use in its onRequest function.
in index.js
1 |
server.start(router.route); |
As you will see later, each request that comes from a browser gets taken care of by onRequest. And onRequest uses the route function in order to show user the url path.
in server.js
1 2 3 4 5 6 7 8 9 10 11 |
//start is called by global variable server in index.js function start(route){ function onRequest(request, response) { ... route(pathname); ... } http.createServer(onRequest).listen(8888); } |
In other words, we pass it in simply so that it is a variable that can be used in the start function. The start function’s function onRequest(request, response) directly uses the route function.
function onRequest(request, response) is just a function definition of start function and does not get used yet.
We have our global variable http and call its function createServer, specifying that we want to create a server and it listens for user requests at port 8888.
We then pass in the onRequest function as a parameter to createServer to let it know what function to use to process when a request comes in.
Start up your servers by opening up a terminal, going into the directory where index.js, server.js, and router.js are located, and in your terminal type:
node index.js
you should see that the server will start.
Rickys-MacBook-Pro:nodeProject1 $ node index.js
start of index.js
start of server.js
end of server.js
start of router.js
end of router.js
server variable running function start with parameter router
server.js – start function:
[Function: route]
server.js – Server has started. end of start function
end of index.js
Then open up a browser and type in:
localhost:8888/test
You will then see result in your terminal:
server.js – start of onRequest function
server.js – request.url: /test
router.js – about to route a request for /test
full code
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
console.log('start of index.js'); //get the module server, which is the server.js we just wrote //put it into the variable server var server = require("./server"); //get the router module, which is router.js we just wrote //put it into the variable router var router = require("./router"); //then have server variable call its function start //using parameter router's router function console.log('server variable running function start with parameter router'); server.start(router.route); console.log('end of index.js'); |
server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
console.log('start of server.js'); //requres the http module that ships with Node.js and return it to //global variable http var http = require("http"); var url = require("url"); //have start function...like bootstrap //start is called by global variable server in index.js function start(route){ console.log('server.js - start function: '); console.log(route); //DEFINES a function called onRequest, to be used repeatedly //by global http's createServer function onRequest(request, response) { console.log('server.js - start of onRequest function'); console.log('server.js - request.url: ' + request.url); var pathname = url.parse(request.url).pathname; route(pathname); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Received request....thanks!"); response.end(); } //uses global variable http and calls method createServer. //method listen for port 8888 //have onRequest with parameter request/response to process //whatever is passed in. //in our case, the URL the user typed in is in request.url //we parse that, then pass it into our route function http.createServer(onRequest).listen(8888); console.log("server.js - Server has started. end of start function"); } exports.start = start; console.log('end of server.js'); |
router.js
1 2 3 4 5 6 7 8 9 10 |
console.log("start of router.js"); function route(pathname) { console.log("router.js - about to route a request for " + pathname); } exports.route = route; console.log("end of router.js"); |