{"action":"created","comment":{"url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/comments/55245175","id":55245175,"diff_hunk":"@@ -1 +1,292 @@\n-# node-modules-patterns\n\\ No newline at end of file\n+# Module Patterns in Node\n+\n+## Overview\n+\n+Imagine a scenario where you have an application with 2,000 lines of code. All the code is in one file. That's a nightmare to navigate around and also difficult for different developers to collaborate without merge conflicts! What if you split the logic into 20 files which each file having 100 lines of code roughly. Much better right? So what's the best way to do that? The answer is modules!\n+\n+Modules allow developers to build better systems because the code can be separated into different files which are grouped into packages or modules. You can write your own module or use a module from an open-source community.\n+\n+This lesson will cover the most common patterns used to create and export a Node module.\n+\n+## Objectives\n+\n+1. Define different patterns for Node modules\n+1. Describe module.exports= {}\n+1. Describe module.exports=function(){}\n+1. Describe exports.method = function(){}\n+1. Describe exports.object = {}\n+\n+## Exporting Objects\n+\n+Consider an example where we have a configuration file with some settings (`configs.js`):\n+\n+```js\n+module.exports = {\n+  url: 'http://webapplog.com',\n+  name: 'React Quicly',\n+  port: 3000,\n+  apiKey: \"D1FCFE58-575F-40FF-AF36-0CBD6A367756\"\n+}\n+```\n+\n+In the `main.js` file (which uses the module), we can import the module and use any settings by accessing the property by the name such as `url` or `name` (`.js` extension is optional):\n+\n+```js\n+var configs = require('./configs')\n+console.log('URL is ' + configs.url)\n+```\n+\n+The result of running `node main` will be the printed URL with the value supplied by the `configs.js`:\n+\n+```\n+URL is http://webapplog.com\n+```\n+\n+We achieved modularity! Now we can have many other files like `program.js` or `app.js` which can import the `config.js` file and access the same values. If we ever need to change `port` or `apiKey`, then it's effortless. We just update `configs.js`. No need to search in all files and risk bugs by missing a few.\n+\n+However, there's a limitation to this approach. Let's say we need to have three sets of API keys: one for development, one for testing and one for production. You can write `if/else` conditions in `main.js` but then you end up repeating the same logic over and over in other files which need to use the configurations.\n+This approach isn't great because we end up with a lot of logic which really belongs to the configuration module spread out across various main files (files which import the module). Take a look at the code below (also in `main-envirionment-bad.js`):","path":"README.md","position":50,"original_position":50,"commit_id":"e17b05d8c0e32947149f6fab6e4d78e7d730f411","original_commit_id":"e17b05d8c0e32947149f6fab6e4d78e7d730f411","user":{"login":"pletcher","id":2015049,"avatar_url":"https://avatars.githubusercontent.com/u/2015049?v=3","gravatar_id":"","url":"https://api.github.com/users/pletcher","html_url":"https://github.com/pletcher","followers_url":"https://api.github.com/users/pletcher/followers","following_url":"https://api.github.com/users/pletcher/following{/other_user}","gists_url":"https://api.github.com/users/pletcher/gists{/gist_id}","starred_url":"https://api.github.com/users/pletcher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pletcher/subscriptions","organizations_url":"https://api.github.com/users/pletcher/orgs","repos_url":"https://api.github.com/users/pletcher/repos","events_url":"https://api.github.com/users/pletcher/events{/privacy}","received_events_url":"https://api.github.com/users/pletcher/received_events","type":"User","site_admin":false},"body":"I like how you go into detail (and show below) why this approach is bad.","created_at":"2016-03-07T18:13:31Z","updated_at":"2016-03-07T18:13:31Z","html_url":"https://github.com/learn-co-curriculum/node-modules-patterns/pull/1#discussion_r55245175","pull_request_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/comments/55245175"},"html":{"href":"https://github.com/learn-co-curriculum/node-modules-patterns/pull/1#discussion_r55245175"},"pull_request":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/1","id":57535993,"html_url":"https://github.com/learn-co-curriculum/node-modules-patterns/pull/1","diff_url":"https://github.com/learn-co-curriculum/node-modules-patterns/pull/1.diff","patch_url":"https://github.com/learn-co-curriculum/node-modules-patterns/pull/1.patch","issue_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues/1","number":1,"state":"open","locked":false,"title":"Wip master","user":{"login":"azat-co","id":1137314,"avatar_url":"https://avatars.githubusercontent.com/u/1137314?v=3","gravatar_id":"","url":"https://api.github.com/users/azat-co","html_url":"https://github.com/azat-co","followers_url":"https://api.github.com/users/azat-co/followers","following_url":"https://api.github.com/users/azat-co/following{/other_user}","gists_url":"https://api.github.com/users/azat-co/gists{/gist_id}","starred_url":"https://api.github.com/users/azat-co/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/azat-co/subscriptions","organizations_url":"https://api.github.com/users/azat-co/orgs","repos_url":"https://api.github.com/users/azat-co/repos","events_url":"https://api.github.com/users/azat-co/events{/privacy}","received_events_url":"https://api.github.com/users/azat-co/received_events","type":"User","site_admin":false},"body":"CC @AnnJohn @vicfriedman ","created_at":"2016-01-28T17:28:46Z","updated_at":"2016-03-07T18:13:31Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e012ab68ab41b2e65ad260ee691bffec2af76beb","assignee":{"login":"pletcher","id":2015049,"avatar_url":"https://avatars.githubusercontent.com/u/2015049?v=3","gravatar_id":"","url":"https://api.github.com/users/pletcher","html_url":"https://github.com/pletcher","followers_url":"https://api.github.com/users/pletcher/followers","following_url":"https://api.github.com/users/pletcher/following{/other_user}","gists_url":"https://api.github.com/users/pletcher/gists{/gist_id}","starred_url":"https://api.github.com/users/pletcher/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pletcher/subscriptions","organizations_url":"https://api.github.com/users/pletcher/orgs","repos_url":"https://api.github.com/users/pletcher/repos","events_url":"https://api.github.com/users/pletcher/events{/privacy}","received_events_url":"https://api.github.com/users/pletcher/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/1/commits","review_comments_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/1/comments","review_comment_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/comments{/number}","comments_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues/1/comments","statuses_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/statuses/e17b05d8c0e32947149f6fab6e4d78e7d730f411","head":{"label":"learn-co-curriculum:wip-master","ref":"wip-master","sha":"e17b05d8c0e32947149f6fab6e4d78e7d730f411","user":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"repo":{"id":50596137,"name":"node-modules-patterns","full_name":"learn-co-curriculum/node-modules-patterns","owner":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/learn-co-curriculum/node-modules-patterns","description":"","fork":false,"url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns","forks_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/forks","keys_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/keys{/key_id}","collaborators_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/teams","hooks_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/hooks","issue_events_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues/events{/number}","events_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/events","assignees_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/assignees{/user}","branches_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/branches{/branch}","tags_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/tags","blobs_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/refs{/sha}","trees_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/trees{/sha}","statuses_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/statuses/{sha}","languages_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/languages","stargazers_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/stargazers","contributors_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/contributors","subscribers_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/subscribers","subscription_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/subscription","commits_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/commits{/sha}","git_commits_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/commits{/sha}","comments_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/comments{/number}","issue_comment_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues/comments{/number}","contents_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/contents/{+path}","compare_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/compare/{base}...{head}","merges_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/merges","archive_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/downloads","issues_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues{/number}","pulls_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls{/number}","milestones_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/milestones{/number}","notifications_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/labels{/name}","releases_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/releases{/id}","deployments_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/deployments","created_at":"2016-01-28T16:23:55Z","updated_at":"2016-01-28T16:23:55Z","pushed_at":"2016-02-26T00:52:39Z","git_url":"git://github.com/learn-co-curriculum/node-modules-patterns.git","ssh_url":"git@github.com:learn-co-curriculum/node-modules-patterns.git","clone_url":"https://github.com/learn-co-curriculum/node-modules-patterns.git","svn_url":"https://github.com/learn-co-curriculum/node-modules-patterns","homepage":null,"size":41,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"learn-co-curriculum:master","ref":"master","sha":"c7204a74efee8d44f74bdaaa9b540cdd18c15755","user":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"repo":{"id":50596137,"name":"node-modules-patterns","full_name":"learn-co-curriculum/node-modules-patterns","owner":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/learn-co-curriculum/node-modules-patterns","description":"","fork":false,"url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns","forks_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/forks","keys_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/keys{/key_id}","collaborators_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/teams","hooks_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/hooks","issue_events_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues/events{/number}","events_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/events","assignees_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/assignees{/user}","branches_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/branches{/branch}","tags_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/tags","blobs_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/refs{/sha}","trees_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/trees{/sha}","statuses_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/statuses/{sha}","languages_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/languages","stargazers_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/stargazers","contributors_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/contributors","subscribers_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/subscribers","subscription_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/subscription","commits_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/commits{/sha}","git_commits_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/git/commits{/sha}","comments_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/comments{/number}","issue_comment_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues/comments{/number}","contents_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/contents/{+path}","compare_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/compare/{base}...{head}","merges_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/merges","archive_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/downloads","issues_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues{/number}","pulls_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls{/number}","milestones_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/milestones{/number}","notifications_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/labels{/name}","releases_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/releases{/id}","deployments_url":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/deployments","created_at":"2016-01-28T16:23:55Z","updated_at":"2016-01-28T16:23:55Z","pushed_at":"2016-02-26T00:52:39Z","git_url":"git://github.com/learn-co-curriculum/node-modules-patterns.git","ssh_url":"git@github.com:learn-co-curriculum/node-modules-patterns.git","clone_url":"https://github.com/learn-co-curriculum/node-modules-patterns.git","svn_url":"https://github.com/learn-co-curriculum/node-modules-patterns","homepage":null,"size":41,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/1"},"html":{"href":"https://github.com/learn-co-curriculum/node-modules-patterns/pull/1"},"issue":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues/1"},"comments":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/learn-co-curriculum/node-modules-patterns/statuses/e17b05d8c0e32947149f6fab6e4d78e7d730f411"}}}}