I’m reading this nice tutorial about Node.js web servers: http://nodebeginner.org/
In this part of the tutorial: http://nodebeginner.org/#whats-needed-to-route-requests they show how to do something called ‘injection dependencies’ in order to link both the route module and the server module to the main js file index.js.
I’ve tried exactly what they write there, but the computer won’t like this. It shows me an error message: route is not defined‘.
I don’t know what to do, please help me with this..I’m pretty new to Node.js and have never seen this kind of dependency injection before.
Thank you!
Here is the contents of index.js:
var server = require("./myHttp");
var router = require("./router");
server.start(router.route);
Here is the myHttp.js file:
var http = require("http");
var url = require("url");
function start(){
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for "+ pathname + " recieved.");
route(pathname);
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
Here is the router.js file:
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;
I run the following command in the cmd: node index.js
server.start(router.route);is called with an argument.function start(){is declared without arguments. The arguments is never used. Why is it there, then?And then, a value of the argument is
router.route. Which is just function itself. It is not called anywhere.