Created
October 24, 2016 04:07
-
-
Save Sysetup/6d086b4bf9a1279a540bf2e31cea0156 to your computer and use it in GitHub Desktop.
Basic NodeJS dependency injection pattern example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var server = require("./server"); | |
| var router = require("./router"); | |
| server.start(router.route); //Injecting router as parameter. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function route(pathname) { | |
| console.log("Route a request for: " + pathname); | |
| } | |
| exports.route = route; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var http = require("http"); | |
| var url = require("url"); | |
| function start(route) { | |
| function onRequest(request, response) { | |
| var pathname = url.parse(request.url).pathname; | |
| console.log("Request from: " + pathname ); | |
| route(pathname); | |
| response.writeHead(200, {"Content-Type": "text/plain"}); | |
| response.write("Response write"); | |
| response.end(); | |
| } | |
| http.createServer(onRequest).listen(8888); | |
| } | |
| exports.start = start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment