├── assets └── images │ ├── placeholdet.txt │ ├── apm1.png │ ├── apm2.png │ ├── apm3.png │ ├── banner-1.png │ ├── banner-2.jpg │ ├── banner-3.jpg │ ├── banner-4.jpg │ ├── circleci.png │ ├── twitter.png │ ├── Privatenpm.png │ ├── Sketch (8).png │ ├── swaggerDoc.png │ ├── twitter-s.png │ ├── viconblue.PNG │ ├── checkbox-sm.png │ ├── monitoring1.png │ ├── monitoring2.jpg │ ├── monitoring3.png │ ├── setnodeenv1.png │ ├── smartlogging1.png │ ├── smartlogging2.jpg │ ├── swaggerMarkup.png │ ├── uptimerobot.jpg │ ├── checkbox-small.PNG │ ├── checkmark-green.png │ ├── testingpyramid.png │ ├── jenkins_dashboard.png │ ├── keepexpressinweb.gif │ ├── structurebyroles.PNG │ ├── utilizecpucores1.png │ ├── checkbox-small-blue.png │ ├── kibana-raw-1024x637.png │ ├── app-dynamics-dashboard.png │ ├── checkmark-green-small.png │ ├── checkmark-green_small.png │ ├── kibana-graph-1024x550.jpg │ ├── structurebycomponents.PNG │ └── createmaintenanceendpoint1.png ├── .gitignore ├── sections ├── projectstructre │ ├── createlayers.chinese.md │ ├── createlayers.md │ ├── configguide.chinese.md │ ├── wraputilities.md │ ├── separateexpress.md │ ├── configguide.md │ ├── thincomponents.md │ ├── breakintcomponents.chinese.md │ └── breakintcomponents.md ├── testingandquality │ ├── bumpversion.md │ └── citools.md ├── template.md ├── production │ ├── detectvulnerabilities.md │ ├── apmproducts.md │ ├── setnodeenv.md │ ├── productoncode.md │ ├── createmaintenanceendpoint.md │ ├── bestateless.md │ ├── measurememory.md │ ├── utilizecpu.md │ ├── assigntransactionid.md │ ├── lockdependencies.md │ ├── guardprocess.md │ ├── frontendout.md │ ├── monitoring.md │ ├── delegatetoproxy.md │ └── smartlogging.md ├── codestylepractices │ └── eslint_prettier.md ├── errorhandling │ ├── documentingusingswagger.md │ ├── testingerrorflows.md │ ├── monitoring.md │ ├── usematurelogger.md │ ├── failfast.md │ ├── apmproducts.md │ ├── catchunhandledpromiserejection.md │ ├── shuttingtheprocess.md │ ├── asyncerrorhandling.md │ ├── operationalvsprogrammererror.md │ ├── centralizedhandling.md │ └── useonlythebuiltinerror.md └── drafts │ ├── readme-general-toc-2.md │ ├── readme-general-toc-1.md │ ├── readme-general-toc-3.md │ └── readme-general-toc-4.md ├── LICENSE └── README.md /assets/images/placeholdet.txt: -------------------------------------------------------------------------------- 1 | lorem ipsum 2 | -------------------------------------------------------------------------------- /assets/images/apm1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/apm1.png -------------------------------------------------------------------------------- /assets/images/apm2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/apm2.png -------------------------------------------------------------------------------- /assets/images/apm3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/apm3.png -------------------------------------------------------------------------------- /assets/images/banner-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/banner-1.png -------------------------------------------------------------------------------- /assets/images/banner-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/banner-2.jpg -------------------------------------------------------------------------------- /assets/images/banner-3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/banner-3.jpg -------------------------------------------------------------------------------- /assets/images/banner-4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/banner-4.jpg -------------------------------------------------------------------------------- /assets/images/circleci.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/circleci.png -------------------------------------------------------------------------------- /assets/images/twitter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/twitter.png -------------------------------------------------------------------------------- /assets/images/Privatenpm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/Privatenpm.png -------------------------------------------------------------------------------- /assets/images/Sketch (8).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/Sketch (8).png -------------------------------------------------------------------------------- /assets/images/swaggerDoc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/swaggerDoc.png -------------------------------------------------------------------------------- /assets/images/twitter-s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/twitter-s.png -------------------------------------------------------------------------------- /assets/images/viconblue.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/viconblue.PNG -------------------------------------------------------------------------------- /assets/images/checkbox-sm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/checkbox-sm.png -------------------------------------------------------------------------------- /assets/images/monitoring1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/monitoring1.png -------------------------------------------------------------------------------- /assets/images/monitoring2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/monitoring2.jpg -------------------------------------------------------------------------------- /assets/images/monitoring3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/monitoring3.png -------------------------------------------------------------------------------- /assets/images/setnodeenv1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/setnodeenv1.png -------------------------------------------------------------------------------- /assets/images/smartlogging1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/smartlogging1.png -------------------------------------------------------------------------------- /assets/images/smartlogging2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/smartlogging2.jpg -------------------------------------------------------------------------------- /assets/images/swaggerMarkup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/swaggerMarkup.png -------------------------------------------------------------------------------- /assets/images/uptimerobot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/uptimerobot.jpg -------------------------------------------------------------------------------- /assets/images/checkbox-small.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/checkbox-small.PNG -------------------------------------------------------------------------------- /assets/images/checkmark-green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/checkmark-green.png -------------------------------------------------------------------------------- /assets/images/testingpyramid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/testingpyramid.png -------------------------------------------------------------------------------- /assets/images/jenkins_dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/jenkins_dashboard.png -------------------------------------------------------------------------------- /assets/images/keepexpressinweb.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/keepexpressinweb.gif -------------------------------------------------------------------------------- /assets/images/structurebyroles.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/structurebyroles.PNG -------------------------------------------------------------------------------- /assets/images/utilizecpucores1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/utilizecpucores1.png -------------------------------------------------------------------------------- /assets/images/checkbox-small-blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/checkbox-small-blue.png -------------------------------------------------------------------------------- /assets/images/kibana-raw-1024x637.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/kibana-raw-1024x637.png -------------------------------------------------------------------------------- /assets/images/app-dynamics-dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/app-dynamics-dashboard.png -------------------------------------------------------------------------------- /assets/images/checkmark-green-small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/checkmark-green-small.png -------------------------------------------------------------------------------- /assets/images/checkmark-green_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/checkmark-green_small.png -------------------------------------------------------------------------------- /assets/images/kibana-graph-1024x550.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/kibana-graph-1024x550.jpg -------------------------------------------------------------------------------- /assets/images/structurebycomponents.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/structurebycomponents.PNG -------------------------------------------------------------------------------- /assets/images/createmaintenanceendpoint1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gaspaonrocks/nodebestpractices/HEAD/assets/images/createmaintenanceendpoint1.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | .idea 3 | .vscode 4 | .idea/**/* 5 | .vscode/**/* 6 | .nyc_output 7 | mochawesome-report 8 | .DS_Store 9 | npm-debug.log.* 10 | node_modules 11 | node_modules/**/* 12 | .eslintcache 13 | cert 14 | logs/* 15 | desktop.ini 16 | package-lock.json 17 | -------------------------------------------------------------------------------- /sections/projectstructre/createlayers.chinese.md: -------------------------------------------------------------------------------- 1 | 应用程序分层,保持Express在其边界内 2 |

3 | 4 | 将组件代码分成web, services, DAL层 5 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/structurebycomponents.PNG "Separate component code into layers") 6 | 7 |

8 | 9 | 1分钟说明:混合层的缺点 10 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/keepexpressinweb.gif "The downside of mixing layers") 11 | -------------------------------------------------------------------------------- /sections/projectstructre/createlayers.md: -------------------------------------------------------------------------------- 1 | # Layer your app, keep Express within its boundaries 2 | 3 |

4 | 5 | ### Separate component code into layers: web, services and DAL 6 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/structurebycomponents.PNG "Separate component code into layers") 7 | 8 |

9 | 10 | ### 1 min explainer: The downside of mixing layers 11 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/keepexpressinweb.gif "The downside of mixing layers") 12 | -------------------------------------------------------------------------------- /sections/testingandquality/bumpversion.md: -------------------------------------------------------------------------------- 1 | # Title here 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | Text 7 | 8 | 9 | ### Code Example – explanation 10 | 11 | ```javascript 12 | code here 13 | ``` 14 | 15 | ### Code Example – another 16 | 17 | ```javascript 18 | code here 19 | ``` 20 | 21 | ### Blog Quote: "Title" 22 | From the blog pouchdb.com, ranked 11 for the keywords “Node Promises” 23 | 24 | > …text here 25 | 26 | ### Image title 27 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/swaggerDoc.png "API error handling") 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /sections/template.md: -------------------------------------------------------------------------------- 1 | # Title here 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | Text 9 | 10 |

11 | 12 | 13 | ### Code Example – explanation 14 | 15 | ```javascript 16 | code here 17 | ``` 18 | 19 |

20 | 21 | ### Code Example – another 22 | 23 | ```javascript 24 | code here 25 | ``` 26 | 27 |

28 | 29 | ### Blog Quote: "Title" 30 | From the blog pouchdb.com, ranked 11 for the keywords “Node Promises” 31 | 32 | > …text here 33 | 34 |

35 | 36 | ### Image title 37 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/swaggerDoc.png "API error handling") 38 | 39 | 40 |

41 | -------------------------------------------------------------------------------- /sections/projectstructre/configguide.chinese.md: -------------------------------------------------------------------------------- 1 | 使用环境感知,安全,分层的配置 2 | 3 |

4 | 5 | 6 | 解释 7 | 8 | 当我们处理配置参数时,常常会很慢并且很烦躁:(1)当需要注入100个keys(而不是只在配置文件中提交它们)时,使用进程环境变量设置所有的keys变得非常繁琐,但是当处理只有devops管理权限的文件时,不改变代码行为就不不会变。一个可靠的配置解决方案必须结合配置文件和进程变量覆盖。(2)枚举一个普通JSON的所有keys时,当目录变得非常庞杂的时候,查找修改条目困难。几乎没有配置库允许将配置存储在多个文件中,运行时将所有文件联合起来。分成几个部分的分层JSON文件能够克服这个问题。请参照下面示例。(3)不推荐存储像密码数据这样的敏感信息,但是又没有快速便捷的方法解决这个难题。一些配置库允许文件加密,其他库在Git提交时加密目录,或者不存储这些目录的真实值,在通过环境变量部署期间枚举真实值。(4)一些高级配置场景需要通过命令行(vargs)注入配置值,或者像Redis一样通过集中缓存同步配置信息,所以不同的服务器不会保存不同的数据。 9 | 10 | 一些配置库可以免费提供这些功能的大部分功能,请查看NPM库([nconf](https://www.npmjs.com/package/nconf) 和 [config](https://www.npmjs.com/package/config))这些库可以满足这些要求中的许多要求。 11 |

12 | 13 | 代码示例-分层配置有助于查找条目和维护庞大的配置文件 14 | javascript 15 | { 16 | // Customer module configs 17 | "Customer": { 18 | "dbConfig": { 19 | "host": "localhost", 20 | "port": 5984, 21 | "dbName": "customers" 22 | }, 23 | "credit": { 24 | "initialLimit": 100, 25 | // Set low for development 26 | "initialDays": 1 27 | } 28 | } 29 | } 30 | 31 |

32 | -------------------------------------------------------------------------------- /sections/projectstructre/wraputilities.md: -------------------------------------------------------------------------------- 1 | # Wrap common utilities as NPM packages 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | Once you start growing and have different components on different servers which consumes similar utilities, you should start managing the dependencies - how can you keep 1 copy of your utility code and let multiple consumer components use and deploy it? well, there is a tool for that, it's called NPM... Start by wrapping 3rd party utility packages with your own code to make it easily replaceable in the future and publish your own code as private NPM package. Now, all your code base can import that code and benefit free dependency management tool. It's possible to publish NPM packages for your own private use without sharing it publicly using [private modules](https://docs.npmjs.com/private-modules/intro), [private registry](https://npme.npmjs.com/docs/tutorials/npm-enterprise-with-nexus.html) or [local NPM packages](https://medium.com/@arnaudrinquin/build-modular-application-with-npm-local-modules-dfc5ff047bcc) 8 | 9 | 10 |

11 | 12 | 13 | ### Sharing your own common utilities across environments and components 14 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/Privatenpm.png "Structuring solution by components") 15 | -------------------------------------------------------------------------------- /sections/production/detectvulnerabilities.md: -------------------------------------------------------------------------------- 1 | # Use tools that automatically detect vulnerable dependencies 2 | 3 |

4 | 5 | ### One Paragraph Explainer 6 | 7 | Modern Node applications have tens and sometimes hundreds of dependencies. If any of the dependencies 8 | you use has a known security vulnerability your app is vulnerable as well. 9 | The following tools automatically check for known security vulnerabilities in your dependencies: 10 | 11 | - [nsp](https://www.npmjs.com/package/nsp) - Node Security Project 12 | - [snyk](https://snyk.io/) - Continuously find & fix vulnerabilities in your dependencies 13 | 14 |

15 | 16 | ### What Other Bloggers Say 17 | From the [StrongLoop](https://strongloop.com/strongblog/best-practices-for-express-in-production-part-one-security/) blog: 18 | 19 | > ...Using to manage your application’s dependencies is powerful and convenient. But the packages that you use may contain critical security vulnerabilities that could also affect your application. The security of your app is only as strong as the “weakest link” in your dependencies. Fortunately, there are two helpful tools you can use to ensure of the third-party packages you use: and requireSafe. These two tools do largely the same thing, so using both might be overkill, but “better safe than sorry” are words to live by when it comes to security... 20 | -------------------------------------------------------------------------------- /sections/codestylepractices/eslint_prettier.md: -------------------------------------------------------------------------------- 1 | # Using ESLint and Prettier 2 | 3 | 4 | ### Comparing ESLint and Prettier 5 | 6 | If you format this code using ESLint, it will just give you a warning that it's too wide (depends on your `max-len` setting). Prettier will automatically format it for you. 7 | 8 | ```javascript 9 | foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne(), noWayYouGottaBeKiddingMe()); 10 | ``` 11 | 12 | ```javascript 13 | foo( 14 | reallyLongArg(), 15 | omgSoManyParameters(), 16 | IShouldRefactorThis(), 17 | isThereSeriouslyAnotherOne(), 18 | noWayYouGottaBeKiddingMe() 19 | ); 20 | ``` 21 | 22 | Source: [https://github.com/prettier/prettier-eslint/issues/101](https://github.com/prettier/prettier-eslint/issues/101) 23 | 24 | ### Integrating ESLint and Prettier 25 | 26 | ESLint and Prettier overlap in the code formatting feature but can be easily combined by using other packages like [prettier-eslint](https://github.com/prettier/prettier-eslint), [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier), and [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier). For more information about their differences, you can view the link [here](https://stackoverflow.com/questions/44690308/whats-the-difference-between-prettier-eslint-eslint-plugin-prettier-and-eslint). 27 | -------------------------------------------------------------------------------- /sections/errorhandling/documentingusingswagger.md: -------------------------------------------------------------------------------- 1 | # Document API errors using Swagger 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | REST APIs return results using HTTP status codes, it’s absolutely required for the API user to be aware not only about the API schema but also about potential errors – the caller may then catch an error and tactfully handle it. For example, your API documentation might state in advanced that HTTP status 409 is returned when the customer name already exist (assuming the API register new users) so the caller can correspondingly render the best UX for the given situation. Swagger is a standard that defines the schema of API documentation offering an eco-system of tools that allow creating documentation easily online, see print screens below 7 | 8 | ### Blog Quote: "You have to tell your callers what errors can happen" 9 | From the blog Joyent, ranked 1 for the keywords “Node.JS logging” 10 | 11 | > We’ve talked about how to handle errors, but when you’re writing a new function, how do you deliver errors to the code that called your function? …If you don’t know what errors can happen or don’t know what they mean, then your program cannot be correct except by accident. So if you’re writing a new function, you have to tell your callers what errors can happen and what they mean… 12 | 13 | 14 | ### Useful Tool: Swagger Online Documentation Creator 15 | ![Swagger API Scheme](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/swaggerDoc.png "API error handling") -------------------------------------------------------------------------------- /sections/production/apmproducts.md: -------------------------------------------------------------------------------- 1 | # Sure user experience with APM products 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | APM (application performance monitoring) refers to a familiy of products that aims to monitor application performance from end to end, also from the customer perspective. While traditional monitoring solutions focuses on Exceptions and standalone technical metrics (e.g. error tracking, slow server endpoints, etc), in real world our app might create disappointed users without any code exceptions, for example if some middleware service performed real slow. APM products measure the user experience from end to end, for example, given a system that encompass frontend UI and multiple distributed services – some APM products can tell how fast a transaction that spans multiple tiers last. It can tell whether the user experience is solid and point to the problem. This attractive offering comes with a relatively high price tag hence it’s recommended for large-scale and complex products that require to go beyond straightforwd monitoring. 9 | 10 |

11 | 12 | 13 | ### APM example – a commercial product that visualize cross-service app performance 14 | 15 | ![APM example](/assets/images/apm1.png "APM example") 16 | 17 |

18 | 19 | ### APM example – a commercial product that emphasize the user experience score 20 | 21 | ![APM example](/assets/images/apm2.png "APM example") 22 | 23 |

24 | 25 | ### APM example – a commercial product that highlights slow code paths 26 | 27 | ![APM example](/assets/images/apm3.png "APM example") 28 | -------------------------------------------------------------------------------- /sections/errorhandling/testingerrorflows.md: -------------------------------------------------------------------------------- 1 | # Test error flows using your favorite test framework 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | Testing ‘happy’ paths is no better than testing failures. Good testing code coverage demands to test exceptional paths. Otherwise, there is no trust that exceptions are indeed handled correctly. Every unit testing framework, like [Mocha](https://mochajs.org/) & [Chai](http://chaijs.com/), supports exception testing (code examples below). If you find it tedious to test every inner function and exception you may settle with testing only REST API HTTP errors. 7 | 8 | 9 | 10 | ### Code example: ensuring the right exception is thrown using Mocha & Chai 11 | 12 | ```javascript 13 | describe("Facebook chat", () => { 14 | it("Notifies on new chat message", () => { 15 | var chatService = new chatService(); 16 | chatService.participants = getDisconnectedParticipants(); 17 | expect(chatService.sendMessage.bind({ message: "Hi" })).to.throw(ConnectionError); 18 | }); 19 | }); 20 | 21 | ``` 22 | 23 | ### Code example: ensuring API returns the right HTTP error code 24 | 25 | ```javascript 26 | it("Creates new Facebook group", function (done) { 27 | var invalidGroupInfo = {}; 28 | httpRequest({ 29 | method: 'POST', 30 | uri: "facebook.com/api/groups", 31 | resolveWithFullResponse: true, 32 | body: invalidGroupInfo, 33 | json: true 34 | }).then((response) => { 35 | // if we were to execute the code in this block, no error was thrown in the operation above 36 | }).catch(function (response) { 37 | expect(400).to.equal(response.statusCode); 38 | done(); 39 | }); 40 | }); 41 | 42 | ``` -------------------------------------------------------------------------------- /sections/production/setnodeenv.md: -------------------------------------------------------------------------------- 1 | # Set NODE_ENV = production 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | Process environment variables is a set of key-value pairs made available to any running program, usually for configuration purposes. Though any variables can be used, Node encourages the convention of using a variable called NODE_ENV to flag whether we’re in production right now. This determination allows components to provide better diagnostics during development, for example by disabling caching or emitting verbose log statements. Any modern deployment tool – Chef, Puppet, CloudFormation, others – support setting environment variables during deployment 9 | 10 |

11 | 12 | 13 | ### Code example: Setting and reading the NODE_ENV environment variable 14 | 15 | ```javascript 16 | // Setting environment variables in bash before starting the node process 17 | $ NODE_ENV=development 18 | $ node 19 | 20 | // Reading the environment variable using code 21 | if (process.env.NODE_ENV === “production”) 22 | useCaching = true; 23 | ``` 24 | 25 |

26 | 27 | 28 | ### What Other Bloggers Say 29 | From the blog [dynatrace](https://www.dynatrace.com/blog/the-drastic-effects-of-omitting-node_env-in-your-express-js-applications/): 30 | > ...In Node.js there is a convention to use a variable called NODE_ENV to set the current mode. We see that it in fact reads NODE_ENV and defaults to ‘development’ if it isn’t set. We clearly see that by setting NODE_ENV to production the number of requests Node.js can handle jumps by around two-thirds while the CPU usage even drops slightly. *Let me emphasize this: Setting NODE_ENV to production makes your application 3 times faster!* 31 | 32 | 33 | ![NODE_ENV=production](/assets/images/setnodeenv1.png "NODE_ENV=production") 34 | 35 | 36 |

37 | -------------------------------------------------------------------------------- /sections/projectstructre/separateexpress.md: -------------------------------------------------------------------------------- 1 | # Separate Express 'app' and 'server' 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | The latest Express generator comes with a great practice that is worth to keep - the API declaration is separated from the network related configuration (port, protocol, etc). This allows testing the API in-process, without performing network calls, with all the benefits that it brings to the table: fast testing execution and getting coverage metrics of the code. It also allows deploying the same API under flexible and different network conditions. Bonus: better separation of concerns and cleaner code 9 | 10 |

11 | 12 | ### Code example: API declaration, should reside in app.js 13 | 14 | ```javascript 15 | var app = express(); 16 | app.use(bodyParser.json()); 17 | app.use("/api/events", events.API); 18 | app.use("/api/forms", forms); 19 | 20 | ``` 21 | 22 |

23 | 24 | ### Code example: Server network declaration, should reside in /bin/www 25 | 26 | ```javascript 27 | var app = require('../app'); 28 | var http = require('http'); 29 | 30 | /** 31 | * Get port from environment and store in Express. 32 | */ 33 | 34 | var port = normalizePort(process.env.PORT || '3000'); 35 | app.set('port', port); 36 | 37 | /** 38 | * Create HTTP server. 39 | */ 40 | 41 | var server = http.createServer(app); 42 | 43 | ``` 44 | 45 | 46 | ### Example: test your API in-process using supertest (popular testing package) 47 | 48 | ```javascript 49 | const app = express(); 50 | 51 | app.get('/user', function(req, res) { 52 | res.status(200).json({ name: 'tobi' }); 53 | }); 54 | 55 | request(app) 56 | .get('/user') 57 | .expect('Content-Type', /json/) 58 | .expect('Content-Length', '15') 59 | .expect(200) 60 | .end(function(err, res) { 61 | if (err) throw err; 62 | }); 63 | ```` 64 | -------------------------------------------------------------------------------- /sections/production/productoncode.md: -------------------------------------------------------------------------------- 1 | # Make your code production-ready 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | Following is a list of development tips that greatly affect the production maintenance and stability: 9 | 10 | * The twelve-factor guide – Get familiar with the [Twelve factors](https://12factor.net/) guide 11 | * Be stateless – Save no data locally on a specific web server (see separate bullet – ‘Be Stateless’) 12 | * Cache – Utilize cache heavily, yet never fail because of cache mismatch 13 | * Test memory – gauge memory usage and leaks as part your development flow, tools such as ‘memwatch’ can greatly facilitate this task 14 | * Name functions – Minimize the usage of anonymous functions (i.e. inline callback) as a typical memory profiler will provide memory usage per method name 15 | * Use CI tools – Use CI tool to detect failures before sending to production. For example, use ESLint to detect reference errors and undefined variables. Use –trace-sync-io to identify code that uses synchronous APIs (instead of the async version) 16 | * Log wisely – Include in each log statement contextual information, hopefully in JSON format so log aggregators tools such as Elastic can search upon those properties (see separate bullet – ‘Increase visibility using smart logs’). Also, include transaction-id that identifies each request and allows to correlate lines that describe the same transaction (see separate bullet – ‘Include Transaction-ID’) 17 | * Error management – Error handling is the Achilles’ heel of Node.JS production sites – many Node processes are crashing because of minor errors while others hang on alive in a faulty state instead of crashing. Setting your error handling strategy is absolutely critical, read here my [error handling best practices](http://goldbergyoni.com/checklist-best-practices-of-node-js-error-handling/) 18 | -------------------------------------------------------------------------------- /sections/production/createmaintenanceendpoint.md: -------------------------------------------------------------------------------- 1 | # Create a maintenance endpoint 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | A maintenance endpoint is a plain secured HTTP API that is part of the app code and its purpose is to be used by the ops/production team to monitor and expose maintenance functionality. For example, it can return a head dump (memory snapshot) of the process, report whether there are some memory leaks and even allow to execute REPL commands directly. This endpoint is needed where the conventional devops tools (monitoring products, logs, etc) fails to gather some specific type of information or you choose not to buy/install such tools. The golden rule is using professional and external tools for monitoring and maintaining the production, these are usually more robust and accurate. That said, there are likely to be cases where the generic tools will fail to extract information that is specific to Node or to your app – for example, should you wish to generate a memory snapshot at the moment GC completed a cycle – few NPM libraries will be glad to perform this for you but popular monitoring tools will be likely to miss this functionality 9 | 10 |

11 | 12 | 13 | ### Code example: generating a head dump via code 14 | 15 | ```javascript 16 | var heapdump = require('heapdump'); 17 | 18 | router.get('/ops/headump', (req, res, next) => { 19 | logger.info('About to generate headump'); 20 | heapdump.writeSnapshot((err, filename) => { 21 | console.log('headump file is ready to be sent to the caller', filename); 22 | fs.readFile(filename, "utf-8", (err, data) => { 23 | res.end(data); 24 | }); 25 | }); 26 | }); 27 | ``` 28 | 29 |

30 | 31 | ### Recommended Resources 32 | 33 | [Getting your Node.js app production ready (Slides)](http://naugtur.pl/pres3/node2prod) 34 | 35 | ▶ [Getting your Node.js app production ready (Video)](https://www.youtube.com/watch?v=lUsNne-_VIk) 36 | 37 | ![Getting your Node.js app production ready](/assets/images/createmaintenanceendpoint1.png "Getting your Node.js app production ready") 38 | -------------------------------------------------------------------------------- /sections/projectstructre/configguide.md: -------------------------------------------------------------------------------- 1 | # Use environment aware, secure and hirearchical config 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | When dealing with configuration data, many things can just annoy and slow down: (1) setting all the keys using process environment variables becomes very tedious when in need to inject 100 keys (instead of just committing those in a config file), however when dealing with files only the devops admins can not alter the behaviour without changing the code. A reliable config solution must combine both configuration files + overrides from the process variables (b) when specifying all keys in a flat JSON, it becomes frustrating to find and modify entries when the list grows bigger. A hierarchical JSON file that is grouped into sections can overcome this issue + few config libraries allow to store the configuration in multiple files and take care to union all in runtime. See example below (3) storing sensitive information like DB password is obviously not recommended but no quick and handy solution exists for this challenge. Some configuration libraries allow to encrypt files, others encrypt those entries during GIT commits or simply don't store real values for those entries and specify the actual value during deployment via environment variables. (4) some advanced configuration scenarios demand to inject configuration values via command line (vargs) or sync configuration info via a centralized cache like Redis so multiple servers will use the same configuration data. 9 | 10 | Some configuration libraries can provide most of these features for free, have a look at NPM libraries like [rc](https://www.npmjs.com/package/rc), [nconf](https://www.npmjs.com/package/nconf) and [config](https://www.npmjs.com/package/config) which tick many of these requirements. 11 | 12 |

13 | 14 | ### Code Example – hirearchical config helps to find entries and maintain huge config files 15 | 16 | ```javascript 17 | { 18 | // Customer module configs 19 | "Customer": { 20 | "dbConfig": { 21 | "host": "localhost", 22 | "port": 5984, 23 | "dbName": "customers" 24 | }, 25 | "credit": { 26 | "initialLimit": 100, 27 | // Set low for development 28 | "initialDays": 1 29 | } 30 | } 31 | } 32 | ``` 33 | 34 |

35 | -------------------------------------------------------------------------------- /sections/testingandquality/citools.md: -------------------------------------------------------------------------------- 1 | # Carefully choose your CI platform 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | The CI world used to be the flexibility of [Jenkins](https://jenkins.io/) vs the simplicity of SaaS vendors. The game is now changing as SaaS providers like [CircleCI](https://circleci.com/) and [Travis](https://travis-ci.org/) offer robust solutions including Docker containers with miniumum setup time while Jenkins tries to compete on 'simplicity' segment as well. Though one can setup rich CI solution in the cloud, should it required to control the finest details Jenkins is still the platform of choice. The choice eventually boils down to which extent the CI process should be customized: free and setup free cloud vendors allow to run custom shell commands, custom docker images, adjust the workflow, run matrix builds and other rich features. However if controlling the infrastructure or programming the CI logic using a formal programming language like Java is desired - Jenkins might still be the choice. Otherwise, consider opting for the simple and setup free cloud option 9 | 10 |

11 | 12 | 13 | ### Code Example – a typical cloud CI configuratin. Single .yml file and that's it 14 | ```javascript 15 | version: 2 16 | jobs: 17 | build: 18 | docker: 19 | - image: circleci/node:4.8.2 20 | - image: mongo:3.4.4 21 | steps: 22 | - checkout 23 | - run: 24 | name: Install npm wee 25 | command: npm install 26 | test: 27 | docker: 28 | - image: circleci/node:4.8.2 29 | - image: mongo:3.4.4 30 | steps: 31 | - checkout 32 | - run: 33 | name: Test 34 | command: npm test 35 | - run: 36 | name: Generate code coverage 37 | command: './node_modules/.bin/nyc report --reporter=text-lcov' 38 | - store_artifacts: 39 | path: coverage 40 | prefix: coverage 41 | 42 | ``` 43 | 44 | 45 | 46 | ### Circle CI - almost zero setup cloud CI 47 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/circleci.png "API error handling") 48 | 49 | ### Jenkins - sophisiticated and robust CI 50 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/jenkins_dashboard.png "API error handling") 51 | 52 | 53 |

54 | -------------------------------------------------------------------------------- /sections/errorhandling/monitoring.md: -------------------------------------------------------------------------------- 1 | # Monitoring 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | > At the very basic level, monitoring means you can *easily identify when bad things happen at production. For example, by getting notified by email or Slack. The challenge is to choose the right set of tools that will satisfy your requirements without breaking your bank. May I suggest, start with defining the core set of metrics that must be watched to ensure a healthy state – CPU, server RAM, Node process RAM (less than 1.4GB), the amount of errors in the last minute, number of process restarts, average response time. Then go over some advanced features you might fancy and add to your wish list. Some examples of luxury monitoring feature: DB profiling, cross-service measuring (i.e. measure business transaction), frontend integration, expose raw data to custom BI clients, Slack notifications and many others. 7 | 8 | Achieving the advanced features demands lengthy setup or buying a commercial product such as Datadog, newrelic and alike. Unfortunately, achieving even the basics is not a walk in the park as some metrics are hardware-related (CPU) and others live within the node process (internal errors) thus all the straightforward tools require some additional setup. For example, cloud vendor monitoring solutions (e.g. AWS CloudWatch, Google StackDriver) will tell you immediately about the hardware metric but nothing about the internal app behavior. On the other end, Log-based solutions such as ElasticSearch lack by default the hardware view. The solution is to augment your choice with missing metrics, for example, a popular choice is sending application logs to Elastic stack and configure some additional agent (e.g. Beat) to share hardware-related information to get the full picture. 9 | 10 | 11 | ### Blog Quote: "We have a problem with promises" 12 | From the blog pouchdb.com, ranked 11 for the keywords “Node Promises” 13 | 14 | > … We recommend you to watch these signals for all of your services: Error Rate: Because errors are user facing and immediately affect your customers. 15 | Response time: Because the latency directly affects your customers and business. 16 | Throughput: The traffic helps you to understand the context of increased error rates and the latency too. 17 | Saturation: It tells how “full” your service is. If the CPU usage is 90%, can your system handle more traffic? 18 | … 19 | -------------------------------------------------------------------------------- /sections/production/bestateless.md: -------------------------------------------------------------------------------- 1 | # Be stateless, kill your Servers almost every day 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | Have you ever encountered a severe production issue where one server was missing some piece of configuration or data? That is probably due to some unnecessary dependency on some local asset that is not part of the deployment. Many successful products treat servers like a phoenix bird – it dies and is reborn periodically without any damage. In other words, a server is just a piece of hardware that executes your code for some time and is replaced after that. 9 | This approach 10 | 11 | - allows to scale by adding and removing servers dynamically without any side-affect 12 | - simplifies the maintenance as it frees our mind from evaluating each server state. 13 | 14 |

15 | 16 | 17 | ### Code example: anti-patterns 18 | 19 | ```javascript 20 | // Typical mistake 1: saving uploaded files locally on a server 21 | var multer = require('multer'); // express middleware for handling multipart uploads 22 | var upload = multer({ dest: 'uploads/' }); 23 | 24 | app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {}); 25 | 26 | // Typical mistake 2: storing authentication sessions (passport) in a local file or memory 27 | var FileStore = require('session-file-store')(session); 28 | app.use(session({ 29 | store: new FileStore(options), 30 | secret: 'keyboard cat' 31 | })); 32 | 33 | // Typical mistake 3: storing information on the global object 34 | Global.someCacheLike.result = { somedata }; 35 | ``` 36 | 37 |

38 | 39 | ### What Other Bloggers Say 40 | From the blog [Martin Fowler](https://martinfowler.com/bliki/PhoenixServer.html): 41 | > ...One day I had this fantasy of starting a certification service for operations. The certification assessment would consist of a colleague and I turning up at the corporate data center and setting about critical production servers with a baseball bat, a chainsaw, and a water pistol. The assessment would be based on how long it would take for the operations team to get all the applications up and running again. This may be a daft fantasy, but there’s a nugget of wisdom here. While you should forego the baseball bats, it is a good idea to virtually burn down your servers at regular intervals. A server should be like a phoenix, regularly rising from the ashes... 42 | 43 |

44 | -------------------------------------------------------------------------------- /sections/production/measurememory.md: -------------------------------------------------------------------------------- 1 | # Measure and guard the memory usage 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | In a perfect world, a web developer shouldn’t deal with memory leaks. In reality, memory issues are a known Node’s gotcha one must be aware of. Above all, memory usage must be monitored constantly. In development and small production sites you may gauge manually using Linux commands or NPM tools and libraries like node-inspector and memwatch. The main drawback of this manual activities is that they require a human being actively monitoring – for serious production sites it’s absolutely vital to use robust monitoring tools e.g. (AWS CloudWatch, DataDog or any similar proactive system) that alerts when a leak happens. There are also few development guidelines to prevent leaks: avoid storing data on the global level, use streams for data with dynamic size, limit variables scope using let and const. 9 | 10 |

11 | 12 | ### What Other Bloggers Say 13 | 14 | * From the blog [Dyntrace](http://apmblog.dynatrace.com/): 15 | > ... ”As we already learned, in Node.js JavaScript is compiled to native code by V8. The resulting native data structures don’t have much to do with their original representation and are solely managed by V8. This means that we cannot actively allocate or deallocate memory in JavaScript. V8 uses a well-known mechanism called garbage collection to address this problem.” 16 | 17 | * From the blog [Dyntrace](http://blog.argteam.com/coding/hardening-node-js-for-production-part-2-using-nginx-to-avoid-node-js-load): 18 | > ... “Although this example leads to obvious results the process is always the same: 19 | Create heap dumps with some time and a fair amount of memory allocation in between 20 | Compare a few dumps to find out what’s growing” 21 | 22 | * From the blog [Dyntrace](http://blog.argteam.com/coding/hardening-node-js-for-production-part-2-using-nginx-to-avoid-node-js-load): 23 | > ... “fault, Node.js will try to use about 1.5GBs of memory, which has to be capped when running on systems with less memory. This is the expected behaviour as garbage collection is a very costly operation. 24 | The solution for it was adding an extra parameter to the Node.js process: 25 | node –max_old_space_size=400 server.js –production ” 26 | “Why is garbage collection expensive? The V8 JavaScript engine employs a stop-the-world garbage collector mechanism. In practice, it means that the program stops execution while garbage collection is in progress.” -------------------------------------------------------------------------------- /sections/errorhandling/usematurelogger.md: -------------------------------------------------------------------------------- 1 | # Use a mature logger to increase errors visibility 2 | 3 | ### One Paragraph Explainer 4 | 5 | We all loovve console.log but obviously a reputable and persisted Logger like [Winston][winston], [Bunyan][bunyan] (highly popular) or [Pino][pino] (the new kid in town which is focused on performance) is mandatory for serious projects. A set of practices and tools will help to reason about errors much quicker – (1) log frequently using different levels (debug, info, error), (2) when logging, provide contextual information as JSON objects, see example below. (3) watch and filter logs using a log querying API (built-in in most loggers) or a log viewer software 6 | (4) Expose and curate log statement for the operation team using operational intelligence tools like Splunk 7 | 8 | [winston]: https://www.npmjs.com/package/winston 9 | [bunyan]: https://www.npmjs.com/package/bunyan 10 | [pino]: https://www.npmjs.com/package/pino 11 | 12 | ### Code Example – Winston Logger in action 13 | 14 | ```javascript 15 | // your centralized logger object 16 | var logger = new winston.Logger({ 17 | level: 'info', 18 | transports: [ 19 | new (winston.transports.Console)(), 20 | new (winston.transports.File)({ filename: 'somefile.log' }) 21 | ] 22 | }); 23 | 24 | // custom code somewhere using the logger 25 | logger.log('info', 'Test Log Message with some parameter %s', 'some parameter', { anything: 'This is metadata' }); 26 | 27 | ``` 28 | 29 | ### Code Example – Querying the log folder (searching for entries) 30 | 31 | ```javascript 32 | var options = { 33 | from: new Date - 24 * 60 * 60 * 1000, 34 | until: new Date, 35 | limit: 10, 36 | start: 0, 37 | order: 'desc', 38 | fields: ['message'] 39 | }; 40 | 41 | 42 | // Find items logged between today and yesterday. 43 | winston.query(options, function (err, results) { 44 | // execute callback with results 45 | }); 46 | 47 | ``` 48 | 49 | ### Blog Quote: "Logger Requirements" 50 | From the blog Strong Loop 51 | 52 | > Lets identify a few requirements (for a logger): 53 | 1. Time stamp each log line. This one is pretty self explanatory – you should be able to tell when each log entry occured. 54 | 2. Logging format should be easily digestible by humans as well as machines. 55 | 3. Allows for multiple configurable destination streams. For example, you might be writing trace logs to one file but when an error is encountered, write to the same file, then into error file and send an email at the same time… 56 | -------------------------------------------------------------------------------- /sections/errorhandling/failfast.md: -------------------------------------------------------------------------------- 1 | # Fail fast, validate arguments using a dedicated library 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | We all know how checking arguments and failing fast is important to avoid hidden bugs (see anti-pattern code example below). If not, read about explicit programming and defensive programming. In reality, we tend to avoid it due to the annoyance of coding it (e.g. think of validating hierarchical JSON object with fields like email and dates) – libraries like Joi and Validator turn this tedious task into a breeze. 7 | 8 | ### Wikipedia: Defensive Programming 9 | 10 | Defensive programming is an approach to improve software and source code, in terms of: General quality – reducing the number of software bugs and problems. Making the source code comprehensible – the source code should be readable and understandable so it is approved in a code audit. Making the software behave in a predictable manner despite unexpected inputs or user actions. 11 | 12 | 13 | 14 | ### Code example: validating complex JSON input using ‘Joi’ 15 | 16 | ```javascript 17 | var memberSchema = Joi.object().keys({ 18 | password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/), 19 | birthyear: Joi.number().integer().min(1900).max(2013), 20 | email: Joi.string().email() 21 | }); 22 | 23 | function addNewMember(newMember) 24 | { 25 | // assertions come first 26 | Joi.assert(newMember, memberSchema); //throws if validation fails 27 | // other logic here 28 | } 29 | 30 | ``` 31 | 32 | ### Anti-pattern: no validation yields nasty bugs 33 | 34 | ```javascript 35 | // if the discount is positive let's then redirect the user to pring his discount coupons 36 | function redirectToPrintDiscount(httpResponse, member, discount) 37 | { 38 | if(discount != 0) 39 | httpResponse.redirect(`/discountPrintView/${member.id}`); 40 | } 41 | 42 | redirectToPrintDiscount(httpResponse, someMember); 43 | // forgot to pass the parameter discount, why the heck was the user redirected to the discount screen? 44 | 45 | ``` 46 | 47 | ### Blog Quote: "You should throw these errors immediately" 48 | From the blog: Joyent 49 | 50 | > A degenerate case is where someone calls an asynchronous function but doesn’t pass a callback. You should throw these errors immediately, since the program is broken and the best chance of debugging it involves getting at least a stack trace and ideally a core file at the point of the error. To do this, we recommend validating the types of all arguments at the start of the function. -------------------------------------------------------------------------------- /sections/production/utilizecpu.md: -------------------------------------------------------------------------------- 1 | # Utilize all CPU cores 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | It might not come as a surprise that at its basic form, Node runs over a single thread=single process=single CPU. Paying for beefy hardware with 4 or 8 CPU and utilizing only one sounds crazy, right? The quickest solution which fits medium sized apps is using Node’s Cluster module which in 10 lines of code spawns a process for each logical core and route requests between the processes in a round-robin style. Even better, use PM2 which sugarcoats the clustering module with a simple interface and cool monitoring UI. While this solution works well for traditional applications, it might fall short for applications that require top-notch performance and robust devops flow. For those advanced use cases, consider replicating the NODE process using custom deployment script and balancing using a specialized tool such as nginx or use a container engine such as AWS ECS or Kubernetees that have advanced features for deployment and replication of processes. 9 | 10 |

11 | 12 | 13 | ### Comparison: Balancing using Node’s cluster vs nginx 14 | 15 | ![Balancing using Node’s cluster vs nginx](/assets/images/utilizecpucores1.png "Balancing using Node’s cluster vs nginx") 16 | 17 |

18 | 19 | ### What Other Bloggers Say 20 | * From the [Node.JS documentation](https://nodejs.org/api/cluster.html#cluster_how_it_works): 21 | > ... The second approach, Node clusters, should, in theory, give the best performance. In practice however, distribution tends to be very unbalanced due to operating system scheduler vagaries. Loads have been observed where over 70% of all connections ended up in just two processes, out of a total of eight ... 22 | 23 | * From the blog [StrongLoop](From the blog StrongLoop): 24 | > ... Clustering is made possible with Node’s cluster module. This enables a master process to spawn worker processes and distribute incoming connections among the workers. However, rather than using this module directly, it’s far better to use one of the many tools out there that does it for you automatically; for example node-pm or cluster-service ... 25 | 26 | * From the Medium post [Node.js process load balance performance: comparing cluster module, iptables and Nginx](https://medium.com/@fermads/node-js-process-load-balancing-comparing-cluster-iptables-and-nginx-6746aaf38272) 27 | > ... Node cluster is simple to implement and configure, things are kept inside Node’s realm without depending on other software. Just remember your master process will work almost as much as your worker processes and with a little less request rate then the other solutions ... -------------------------------------------------------------------------------- /sections/projectstructre/thincomponents.md: -------------------------------------------------------------------------------- 1 | # Structure your solution by components 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | For medium sized apps and above, monoliths are really bad - a one big software with many dependencies is just hard to reason about and often leads to code spaghetti. Even those smart architects who are skilled to tame the beast and 'modularize' it - spend great mental effort on design and each change requires to carefully evaluate the impact on other dependant objects. The ultimate solution is to develop small software: divide the whole stack into self-contained components that don't share files with others, each constitute very few files (e.g. API, service, data access, test, etc) so that it's very easy to reason about it. Some may call this 'microservices' architecture - it's important to understand that microservices is not a spec which you must follow rather a set of principles. You may adopt many principles into a full-blown microservices architecture or adopt only few. Both are good as long as you keep the software complexity low. The very least you should do is create a basic borders between components, assign a folder in your project root for each business component and make it self contained - other components are allowed to consumeits functionality only through its public interface or API. This is the foundation for keeping your components simple, avoid dependencies hell and pave the way to full-blown microservices in the future once your app grows 9 | 10 |

11 | 12 | 13 | ### Blog Quote: "Scaling requires scaling of the entire application" 14 | From the blog MartinFowler.com 15 | 16 | > Monolithic applications can be successful, but increasingly people are feeling frustrations with them - especially as more applications are being deployed to the cloud . Change cycles are tied together - a change made to a small part of the application, requires the entire monolith to be rebuilt and deployed. Over time it's often hard to keep a good modular structure, making it harder to keep changes that ought to only affect one module within that module. Scaling requires scaling of the entire application rather than parts of it that require greater resource. 17 | 18 |

19 | 20 | ### Good: Structure your solution by self-contained components 21 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/structurebycomponents.PNG "Structuring solution by components") 22 | 23 |

24 | 25 | ### Bad: Group your files by technical role 26 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/structurebyroles.PNG "Structuring solution by technical roles") 27 | -------------------------------------------------------------------------------- /sections/production/assigntransactionid.md: -------------------------------------------------------------------------------- 1 | # Assign ‘TransactionId’ to each log statement 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | A typical log is a warehouse of entries from all components and requests. Upon detection of some suspicious line or error it becomes hairy to match other lines that belong to the same specific flow (e.g. the user “John” tried to buy something). This becomes even more critical and challenging in a microservice environment when a request/transaction might span across multiple computers. Address this by assigning a unique transaction identifier value to all the entries from the same request so when detecting one line one can copy the id and search for every line that has similar transaction Id. However, achieving this In Node is not straightforward as a single thread is used to serve all requests –consider using a library that that can group data on the request level – see code example on the next slide. When calling other microservice, pass the transaction Id using an HTTP header like “x-transaction-id” to keep the same context. 9 | 10 |

11 | 12 | 13 | ### Code example: typical Express configuration 14 | 15 | ```javascript 16 | // when receiving a new request, start a new isolated context and set a transaction Id. The following example is using the NPM library continuation-local-storage to isolate requests 17 | 18 | const { createNamespace } = require('continuation-local-storage'); 19 | var session = createNamespace('my session'); 20 | 21 | router.get('/:id', (req, res, next) => { 22 | session.set('transactionId', 'some unique GUID'); 23 | someService.getById(req.params.id); 24 | logger.info('Starting now to get something by Id'); 25 | }); 26 | 27 | // Now any other service or components can have access to the contextual, per-request, data 28 | class someService { 29 | getById(id) { 30 | logger.info(“Starting to get something by Id”); 31 | // other logic comes here 32 | } 33 | } 34 | 35 | // The logger can now append the transaction-id to each entry, so that entries from the same request will have the same value 36 | class logger { 37 | info (message) 38 | {console.log(`${message} ${session.get('transactionId')}`);} 39 | } 40 | ``` 41 | 42 |

43 | 44 | ### What Other Bloggers Say 45 | From the blog [ARG! TEAM](http://blog.argteam.com/coding/hardening-node-js-for-production-part-2-using-nginx-to-avoid-node-js-load): 46 | > ...Although express.js has built in static file handling through some connect middleware, you should never use it. *Nginx can do a much better job of handling static files and can prevent requests for non-dynamic content from clogging our node processes*... 47 | -------------------------------------------------------------------------------- /sections/projectstructre/breakintcomponents.chinese.md: -------------------------------------------------------------------------------- 1 | # Structure your solution by components 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | For medium sized apps and above, monoliths are really bad - a one big software with many dependencies is just hard to reason about and often lead to code spaghetti. Even those smart architects who are skilled to tame the beast and 'modularize' it - spend great mental effort on design and each change requires to carefully evaluate the impact on other dependant objects. The ultimate solution is to develop small software: divide the whole stack into self-contained components that don't share files with others, each constitute very few files (e.g. API, service, data access, test, etc) so that it's very easy to reason about it. Some may call this 'microservices' architecture - it's important to understand that microservices is not a spec which you must follow rather a set of principles. You may adopt many principles into a full-blown microservices architecture or adopt only few. Both are good as long as you keep the software complexity low. The very least you should do is create a basic borders between components, assign a folder in your project root for each business component and make it self contained - other components are allowed to consume its functionality only through its public interface or API. This is the foundation for keeping your components simple, avoid dependencies hell and pave the way to full-blown microservices in the future once your app grows. 9 | 10 |

11 | 12 | 13 | ### Blog Quote: "Scaling requires scaling of the entire application" 14 | From the blog MartinFowler.com 15 | 16 | > Monolithic applications can be successful, but increasingly people are feeling frustrations with them - especially as more applications are being deployed to the cloud . Change cycles are tied together - a change made to a small part of the application, requires the entire monolith to be rebuilt and deployed. Over time it's often hard to keep a good modular structure, making it harder to keep changes that ought to only affect one module within that module. Scaling requires scaling of the entire application rather than parts of it that require greater resource. 17 | 18 |

19 | 20 | ### Good: Structure your solution by self-contained components 21 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/structurebycomponents.PNG "Structuring solution by components") 22 | 23 |

24 | 25 | ### Bad: Group your files by technical role 26 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/structurebyroles.PNG "Structuring solution by technical roles") 27 | -------------------------------------------------------------------------------- /sections/production/lockdependencies.md: -------------------------------------------------------------------------------- 1 | # Lock dependencies 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | 9 | 10 | Your code depends on many external packages, let’s say it ‘requires’ and use momentjs-2.1.4, then by default when you deploy to production NPM might fetch momentjs 2.1.5 which unfortunately brings some new bugs to the table. Using NPM config files and the argument ```–save-exact=true``` instructs NPM to refer to the *exact* same version that was installed so the next time you run ```npm install``` (in production or within a Docker container you plan to ship forward for testing) the same dependent version will be fetched. An alternative and popular approach is using a .shrinkwrap file (easily generated using NPM) that states exactly which packages and versions should be installed so no environement can get tempted to fetch newer versions than expected. 11 | 12 | * **Update:** as of NPM 5, dependencies are locked automatically using .shrinkwrap. Yarn, an emerging package manager, also locks down dependencies by default 13 | 14 | 15 |

16 | 17 | 18 | ### Code example: .npmrc file that instructs NPM to use exact versions 19 | 20 | ``` 21 | // save this as .npmrc file on the project directory 22 | save-exact:true 23 | ``` 24 | 25 |

26 | 27 | ### Code example: shirnkwrap.json file that distill the exact depedency tree 28 | 29 | ```javascript 30 | { 31 | "name": "A", 32 | "dependencies": { 33 | "B": { 34 | "version": "0.0.1", 35 | "dependencies": { 36 | "C": { 37 | "version": "0.1.0" 38 | } 39 | } 40 | } 41 | } 42 | } 43 | ``` 44 | 45 |

46 | 47 | ### Code example: NPM 5 dependencies lock file – package.json 48 | 49 | ```javascript 50 | { 51 | "name": "package-name", 52 | "version": "1.0.0", 53 | "lockfileVersion": 1, 54 | "dependencies": { 55 | "cacache": { 56 | "version": "9.2.6", 57 | "resolved": "https://registry.npmjs.org/cacache/-/cacache-9.2.6.tgz", 58 | "integrity": "sha512-YK0Z5Np5t755edPL6gfdCeGxtU0rcW/DBhYhYVDckT+7AFkCCtedf2zru5NRbBLFk6e7Agi/RaqTOAfiaipUfg==" 59 | }, 60 | "duplexify": { 61 | "version": "3.5.0", 62 | "resolved": "https://registry.npmjs.org/duplexify/-/duplexify-3.5.0.tgz", 63 | "integrity": "sha1-GqdzAC4VeEV+nZ1KULDMquvL1gQ=", 64 | "dependencies": { 65 | "end-of-stream": { 66 | "version": "1.0.0", 67 | "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.0.0.tgz", 68 | "integrity": "sha1-1FlucCc0qT5A6a+GQxnqvZn/Lw4=" 69 | } 70 | } 71 | } 72 | } 73 | } 74 | ``` 75 | -------------------------------------------------------------------------------- /sections/production/guardprocess.md: -------------------------------------------------------------------------------- 1 | # Guard and restart your process upon failure (using the right tool) 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | At the base level, Node processes must be guarded and restarted upon failures. Simply put, for small apps and those who don’t use containers – tools like [PM2](https://www.npmjs.com/package/pm2-docker) are perfect as they bring simplicity, restarting capabilities and also rich integration with Node. Others with strong Linux skills might use systemd and run Node as a service. Things get more interesting for apps that use Docker or any container technology since those are usually accompanied by cluster management and orchestration tools (e.g. [AWS ECS](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html), [Kubernetes](https://kubernetes.io/), etc) that deploy, monitor and heal containers. Having all those rich cluster management features including container restart, why mess up with other tools like PM2? There’s no bullet proof answer. There are good reasons to keep PM2 within containers (mostly its containers specific version [pm2-docker](https://www.npmjs.com/package/pm2-docker)) as the first guarding tier – it’s much faster to restart a process and provide Node-specific features like flagging to the code when the hosting container asks to gracefully restart. Other might choose to avoid unnecessary layers. To conclude this write-up, no solution suits them all and getting to know the options is the important thing 9 | 10 |

11 | 12 | 13 | ### What Other Bloggers Say 14 | 15 | * From the [Express Production Best Practices](https://expressjs.com/en/advanced/best-practice-performance.html): 16 | > ... In development, you started your app simply from the command line with node server.js or something similar. **But doing this in production is a recipe for disaster. If the app crashes, it will be offline** until you restart it. To ensure your app restarts if it crashes, use a process manager. A process manager is a “container” for applications that facilitates deployment, provides high availability, and enables you to manage the application at runtime. 17 | 18 | * From the Medium blog post [Understanding Node Clustering](https://medium.com/@CodeAndBiscuits/understanding-nodejs-clustering-in-docker-land-64ce2306afef#.cssigr5z3): 19 | > ... Understanding NodeJS Clustering in Docker-Land “Docker containers are streamlined, lightweight virtual environments, designed to simplify processes to their bare minimum. Processes that manage and coordinate their own resources are no longer as valuable. **Instead, management stacks like Kubernetes, Mesos, and Cattle have popularized the concept that these resources should be managed infrastructure-wide**. CPU and memory resources are allocated by “schedulers”, and network resources are managed by stack-provided load balancers. 20 | -------------------------------------------------------------------------------- /sections/errorhandling/apmproducts.md: -------------------------------------------------------------------------------- 1 | # Discover errors and downtime using APM products 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | Exception != Error. Traditional error handling assumes the existence of Exception but application errors might come in the form of slow code paths, API downtime, lack of computational resources and more. This is where APM products come in handy as they allow to detect a wide variety of ‘burried’ issues proactively with a minimal setup. Among the common features of APM products are for example alerting when the HTTP API returns errors, detect when the API response time drops below some threshold, detection of ‘code smells’, features to monitor server resources, operational intelligence dashboard with IT metrics and many other useful features. Most vendors offer a free plan. 7 | 8 | ### Wikipedia about APM 9 | 10 | In the fields of information technology and systems management, Application Performance Management (APM) is the monitoring and management of performance and availability of software applications. APM strives to detect and diagnose complex application performance problems to maintain an expected level of service. APM is “the translation of IT metrics into business meaning ([i.e.] value) 11 | Major products and segments 12 | 13 | ### Understanding the APM marketplace 14 | 15 | APM products constitues 3 major segments: 16 | 17 | 1. Website or API monitoring – external services that constantly monitor uptime and performance via HTTP requests. Can be set up in few minutes. Following are few selected contenders: [Pingdom](https://www.pingdom.com/), [Uptime Robot](https://uptimerobot.com/), and [New Relic](https://newrelic.com/application-monitoring) 18 | 19 | 2. Code instrumentation – product family which require to embed an agent within the application to use features like slow code detection, exception statistics, performance monitoring and many more. Following are few selected contenders: New Relic, App Dynamics 20 | 21 | 3. Operational intelligence dashboard – this line of products is focused on facilitating the ops team with metrics and curated content that helps to easily stay on top of application performance. This usually involves aggregating multiple sources of information (application logs, DB logs, servers log, etc) and upfront dashboard design work. Following are few selected contenders: [Datadog](https://www.datadoghq.com/), [Splunk](https://www.splunk.com/), [Zabbix](https://www.zabbix.com/) 22 | 23 | 24 | 25 | ### Example: UpTimeRobot.Com – Website monitoring dashboard 26 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/uptimerobot.jpg "Website monitoring dashboard") 27 | 28 | ### Example: AppDynamics.Com – end to end monitoring combined with code instrumentation 29 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/app-dynamics-dashboard.png "end to end monitoring combined with code instrumentation") 30 | -------------------------------------------------------------------------------- /sections/errorhandling/catchunhandledpromiserejection.md: -------------------------------------------------------------------------------- 1 | # Catch unhandled promise rejections 2 |

3 | 4 | 5 | ### One Paragraph Explainer 6 | 7 | Typically, most of modern Node.JS/Express application code runs within promises – whether within the .then handler, a function callback or in a catch block. Suprisingly, unless a developer remembered to add a .catch clause, errors thrown at these places are not handled by the uncaughtException event-handler and disappear. Recent versions of Node added a warning message when an unhandled rejection pops, though this might help to notice when things go wrong but it's obviously not a proper error handling method. The straightforward solution is to never forget adding .catch clauses within each promise chain call and redirect to a centralized error handler. However building your error handling strategy only on developer’s discipline is somewhat fragile. Consequently, it’s highly recommended using a graceful fallback and subscribe to `process.on(‘unhandledRejection’, callback)` – this will ensure that any promise error, if not handled locally, will get its treatment. 8 | 9 |

10 | 11 | ### Code example: these errors will not get caught by any error handler (except unhandledRejection) 12 | 13 | ```javascript 14 | DAL.getUserById(1).then((johnSnow) => { 15 | // this error will just vanish 16 | if(johnSnow.isAlive == false) 17 | throw new Error('ahhhh'); 18 | }); 19 | 20 | ``` 21 |

22 | ### Code example: Catching unresolved and rejected promises 23 | 24 | ```javascript 25 | process.on('unhandledRejection', (reason, p) => { 26 | // I just caught an unhandled promise rejection, since we already have fallback handler for unhandled errors (see below), let throw and let him handle that 27 | throw reason; 28 | }); 29 | process.on('uncaughtException', (error) => { 30 | // I just received an error that was never handled, time to handle it and then decide whether a restart is needed 31 | errorManagement.handler.handleError(error); 32 | if (!errorManagement.handler.isTrustedError(error)) 33 | process.exit(1); 34 | }); 35 | 36 | ``` 37 |

38 | ### Blog Quote: "If you can make a mistake, at some point you will" 39 | From the blog James Nelson 40 | 41 | > Let’s test your understanding. Which of the following would you expect to print an error to the console? 42 | 43 | ```javascript 44 | Promise.resolve(‘promised value’).then(() => { 45 | throw new Error(‘error’); 46 | }); 47 | 48 | Promise.reject(‘error value’).catch(() => { 49 | throw new Error(‘error’); 50 | }); 51 | 52 | new Promise((resolve, reject) => { 53 | throw new Error(‘error’); 54 | }); 55 | ``` 56 | 57 | > I don’t know about you, but my answer is that I’d expect all of them to print an error. However, the reality is that a number of modern JavaScript environments won’t print errors for any of them.The problem with being human is that if you can make a mistake, at some point you will. Keeping this in mind, it seems obvious that we should design things in such a way that mistakes hurt as little as possible, and that means handling errors by default, not discarding them. 58 | -------------------------------------------------------------------------------- /sections/errorhandling/shuttingtheprocess.md: -------------------------------------------------------------------------------- 1 | # Exit the process gracefully when a stranger comes to town 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | Somewhere within your code, an error handler object is responsible for deciding how to proceed when an error is thrown – if the error is trusted (i.e. operational error, see further explanation within best practice #3) then writing to log file might be enough. Things get hairy if the error is not familiar – this means that some component might be in a faulty state and all future requests are subject to failure. For example, assuming a singleton, stateful token issuer service that threw an exception and lost its state – from now it might behave unexpectedly and cause all requests to fail. Under this scenario, kill the process and use a ‘Restarter tool’ (like Forever, PM2, etc) to start over with a clean slate. 7 | 8 | 9 | 10 | ### Code example: deciding whether to crash 11 | 12 | ```javascript 13 | // Assuming developers mark known operational errors with error.isOperational=true, read best practice #3 14 | process.on('uncaughtException', function(error) { 15 | errorManagement.handler.handleError(error); 16 | if(!errorManagement.handler.isTrustedError(error)) 17 | process.exit(1) 18 | }); 19 | 20 | 21 | // centralized error handler encapsulates error-handling related logic 22 | function errorHandler(){ 23 | this.handleError = function (error) { 24 | return logger.logError(err).then(sendMailToAdminIfCritical).then(saveInOpsQueueIfCritical).then(determineIfOperationalError); 25 | } 26 | 27 | this.isTrustedError = function (error) { 28 | return error.isOperational; 29 | } 30 | 31 | ``` 32 | 33 | 34 | ### Blog Quote: "The best way is to crash" 35 | From the blog Joyent 36 | 37 | > …The best way to recover from programmer errors is to crash immediately. You should run your programs using a restarter that will automatically restart the program in the event of a crash. With a restarter in place, crashing is the fastest way to restore reliable service in the face of a transient programmer error… 38 | 39 | 40 | ### Blog Quote: "There are three schools of thoughts on error handling" 41 | From the blog: JS Recipes 42 | 43 | > …There are primarily three schools of thoughts on error handling: 44 | 1. Let the application crash and restart it. 45 | 2. Handle all possible errors and never crash. 46 | 3. Balanced approach between the two 47 | 48 | 49 | ### Blog Quote: "No safe way to leave without creating some undefined brittle state" 50 | From Node.JS official documentation 51 | 52 | > …By the very nature of how throw works in JavaScript, there is almost never any way to safely “pick up where you left off”, without leaking references, or creating some other sort of undefined brittle state. The safest way to respond to a thrown error is to shut down the process. Of course, in a normal web server, you might have many connections open, and it is not reasonable to abruptly shut those down because an error was triggered by someone else. The better approach is to send an error response to the request that triggered the error, while letting the others finish in their normal time, and stop listening for new requests in that worker. -------------------------------------------------------------------------------- /sections/production/frontendout.md: -------------------------------------------------------------------------------- 1 | # Get your frontend assets out of Node 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | In a classic web app the backend serves the frontend/graphics to the browser, a very common approach in the Node’s world is to use Express static middleware for streamlining static files to the client. BUT – Node is not a typical webapp as it utilizes a single thread that is not optimized to serve many files at once. Instead, consider using a reverse proxy (e.g. nginx, HAProxy), cloud storage or CDN (e.g. AWS S3, Azure Blob Storage, etc) that utilizes many optimizations for this task and gain much better throughput. For example, specialized middleware like nginx embodies direct hooks between the file system and the network card and uses a multi-threaded approach to minimize intervention among multiple requests. 9 | 10 | Your optimal solution might wear one of the following forms: 11 | 12 | 1. Using a reverse proxy – your static files will be located right next to your Node application, only requests to the static files folder will be served by a proxy that sits in front of your Node app such as nginx. Using this approach, your Node app is responsible deploying the static files but not to serve them. Your frontend’s colleague will love this approach as it prevents cross-origin-requests from the frontend. 13 | 14 | 2. Cloud storage – your static files will NOT be part of your Node app content, they will be uploaded to services like AWS S3, Azure BlobStorage, or other similar services that were born for this mission. Using this approach, your Node app is not responsible deploying the static files neither to serve them, hence a complete decoupling is drawn between Node and the Frontend which is anyway handled by different teams. 15 | 16 |

17 | 18 | 19 | ### Configuration example: typical nginx configuration for serving static files 20 | 21 | ``` 22 | # configure gzip compression 23 | gzip on; 24 | keepalive 64; 25 | 26 | # defining web server 27 | server { 28 | listen 80; 29 | listen 443 ssl; 30 | 31 | # handle static content 32 | location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) { 33 | root /usr/local/silly_face_society/node/public; 34 | access_log off; 35 | expires max; 36 | } 37 | ``` 38 | 39 |

40 | 41 | ### What Other Bloggers Say 42 | From the blog [StrongLoop](https://strongloop.com/strongblog/best-practices-for-express-in-production-part-two-performance-and-reliability/): 43 | 44 | >…In development, you can use [res.sendFile()](http://expressjs.com/4x/api.html#res.sendFile) to serve static files. But don’t do this in production, because this function has to read from the file system for every file request, so it will encounter significant latency and affect the overall performance of the app. Note that res.sendFile() is not implemented with the sendfile system call, which would make it far more efficient. Instead, use serve-static middleware (or something equivalent), that is optimized for serving files for Express apps. An even better option is to use a reverse proxy to serve static files; see Use a reverse proxy for more information… 45 | 46 |

47 | -------------------------------------------------------------------------------- /sections/production/monitoring.md: -------------------------------------------------------------------------------- 1 | # Monitoring! 2 | 3 |

4 | 5 | ### One Paragraph Explainer 6 | 7 | At the very basic level, monitoring means you can *easily* identify when bad things happen at production. For example, by getting notified by email or Slack. The challenge is to choose the right set of tools that will satisfy your requirements without breaking your bank. May I suggest, start with defining the core set of metrics that must be watched to ensure a healthy state – CPU, server RAM, Node process RAM (less than 1.4GB), the amount of errors in the last minute, number of process restarts, average response time. Then go over some advanced features you might fancy and add to your wish list. Some examples of luxury monitoring feature: DB profiling, cross-service measuring (i.e. measure business transaction), frontend integration, expose raw data to custom BI clients, Slack notifications and many others. 8 | 9 | Achieving the advanced features demands lengthy setup or buying a commercial product such as Datadog, NewRelic and alike. Unfortunately, achieving even the basics is not a walk in the park as some metrics are hardware-related (CPU) and others live within the node process (internal errors) thus all the straightforward tools require some additional setup. For example, cloud vendor monitoring solutions (e.g. [AWS CloudWatch](https://aws.amazon.com/cloudwatch/), [Google StackDriver](https://cloud.google.com/stackdriver/)) will tell you immediately about the hardware metrics but not about the internal app behavior. On the other end, Log-based solutions such as ElasticSearch lack the hardware view by default. The solution is to augment your choice with missing metrics, for example, a popular choice is sending application logs to [Elastic stack](https://www.elastic.co/products) and configure some additional agent (e.g. [Beat](https://www.elastic.co/products)) to share hardware-related information to get the full picture. 10 | 11 | 12 |

13 | 14 | 15 | ### Monitoring example: AWS cloudwatch default dashboard. Hard to extract in-app metrics 16 | 17 | ![AWS cloudwatch default dashboard. Hard to extract in-app metrics](/assets/images/monitoring1.png) 18 | 19 |

20 | 21 | ### Monitoring example: StackDriver default dashboard. Hard to extract in-app metrics 22 | 23 | ![StackDriver default dashboard. Hard to extract in-app metrics](/assets/images/monitoring2.jpg) 24 | 25 |

26 | 27 | ### Monitoring example: Grafana as the UI layer that visualizes raw data 28 | 29 | ![Grafana as the UI layer that visualizes raw data](/assets/images/monitoring3.png) 30 | 31 |

32 | ### What Other Bloggers Say 33 | From the blog [Rising Stack](http://mubaloo.com/best-practices-deploying-node-js-applications/): 34 | 35 | > …We recommend you to watch these signals for all of your services: 36 | > Error Rate: Because errors are user facing and immediately affect your customers. 37 | > Response time: Because the latency directly affects your customers and business. 38 | > Throughput: The traffic helps you to understand the context of increased error rates and the latency too. 39 | > Saturation: It tells how “full” your service is. If the CPU usage is 90%, can your system handle more traffic? … 40 | -------------------------------------------------------------------------------- /sections/production/delegatetoproxy.md: -------------------------------------------------------------------------------- 1 | # Delegate anything possible (e.g. static content, gzip) to a reverse proxy 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | It’s very tempting to cargo-cult Express and use its rich middleware offering for networking related tasks like serving static files, gzip encoding, throttling requests, SSL termination, etc. This is a performance kill due to its single threaded model which will keep the CPU busy for long periods (Remember, Node’s execution model is optimized for short tasks or async IO related tasks). A better approach is to use a tool that expertise in networking tasks – the most popular are nginx and HAproxy which are also used by the biggest cloud vendors to lighten the incoming load on node.js processes. 9 | 10 |

11 | 12 | 13 | ### Nginx Config Example – Using nginx to compress server responses 14 | 15 | ``` 16 | # configure gzip compression 17 | gzip on; 18 | gzip_comp_level 6; 19 | gzip_vary on; 20 | 21 | # configure upstream 22 | upstream myApplication { 23 | server 127.0.0.1:3000; 24 | server 127.0.0.1:3001; 25 | keepalive 64; 26 | } 27 | 28 | #defining web server 29 | server { 30 | # configure server with ssl and error pages 31 | listen 80; 32 | listen 443 ssl; 33 | ssl_certificate /some/location/sillyfacesociety.com.bundle.crt; 34 | error_page 502 /errors/502.html; 35 | 36 | # handling static content 37 | location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) { 38 | root /usr/local/silly_face_society/node/public; 39 | access_log off; 40 | expires max; 41 | } 42 | ``` 43 | 44 |

45 | 46 | ### What Other Bloggers Say 47 | 48 | * From the blog [Mubaloo](http://mubaloo.com/best-practices-deploying-node-js-applications): 49 | > …It’s very easy to fall into this trap – You see a package like Express and think “Awesome! Let’s get started” – you code away and you’ve got an application that does what you want. This is excellent and, to be honest, you’ve won a lot of the battle. However, you will lose the war if you upload your app to a server and have it listen on your HTTP port, because you’ve forgotten a very crucial thing: Node is not a web server. **As soon as any volume of traffic starts to hit your application, you’ll notice that things start to go wrong: connections are dropped, assets stop being served or, at the very worst, your server crashes. What you’re doing is attempting to have Node deal with all of the complicated things that a proven web server does really well. Why reinvent the wheel?** 50 | > **This is just for one request, for one image and bearing in mind this is memory that your application could be using for important stuff like reading a database or handling complicated logic; why would you cripple your application for the sake of convenience?** 51 | 52 | 53 | * From the blog [Argteam](http://blog.argteam.com/coding/hardening-node-js-for-production-part-2-using-nginx-to-avoid-node-js-load): 54 | > Although express.js has built in static file handling through some connect middleware, you should never use it. **Nginx can do a much better job of handling static files and can prevent requests for non-dynamic content from clogging our node processes**… 55 | -------------------------------------------------------------------------------- /sections/production/smartlogging.md: -------------------------------------------------------------------------------- 1 | # Make your app transparent using smart logs 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | Since you print out log statements anyway and you're obviously in a need of some interface that wraps up production information where you can trace errors and core metrics (e.g. how many errors happen every hour and which is your slowest API end-point) why not invest some moderate effort in a robust logging framework that will tick all boxes? Achieving that requires a thoughtful decision on three steps: 9 | 10 | **1. smart logging** – at the bare minimum you need to use a reputable logging library like [Winston](https://github.com/winstonjs/winston), [Bunyan](https://github.com/trentm/node-bunyan) and write meaningful information at each transaction start and end. Consider to also format log statements as JSON and provide all the contextual properties (e.g. user id, operation type, etc) so that the operations team can act on those fields. Include also a unique transaction ID at each log line, for more information refer to the bullet below “Write transaction-id to log”. One last point to consider is also including an agent that logs the system resource like memory and CPU like Elastic Beat. 11 | 12 | **2. smart aggregation** – once you have comprehensive information within your servers file system, it’s time to periodically push these to a system that aggregates, facilities and visualizes this data. The Elastic stack, for example, is a popular and free choice that offers all the components to aggregate and visualize data. Many commercial products provide similar functionality only they greatly cut down the setup time and require no hosting. 13 | 14 | **3. smart visualization** – now the information is aggregated and searchable, one can be satisfied only with the power of easily searching the logs but this can go much further without coding or spending much effort. We can now show important operational metrics like error rate, average CPU throughout the day, how many new users opted-in in the last hour and any other metric that helps to govern and improve our app 15 | 16 |

17 | 18 | 19 | ### Visualization Example: Kibana (part of Elastic stack) facilitates advanced searching on log content 20 | 21 | ![Kibana facilitates advanced searching on log content](/assets/images/smartlogging1.png "Kibana facilitates advanced searching on log content") 22 | 23 |

24 | 25 | ### Visualization Example: Kibana (part of Elastic stack) visualizes data based on logs 26 | 27 | ![Kibana visualizes data based on logs](/assets/images/smartlogging2.jpg "Kibana visualizes data based on logs") 28 | 29 |

30 | 31 | ### Blog Quote: Logger Requirements 32 | From the blog [Strong Loop](https://strongloop.com/strongblog/compare-node-js-logging-winston-bunyan/): 33 | 34 | > Lets identify a few requirements (for a logger): 35 | > 1. Time stamp each log line. This one is pretty self explanatory – you should be able to tell when each log entry occured. 36 | > 2. Logging format should be easily digestible by humans as well as machines. 37 | > 3. Allows for multiple configurable destination streams. For example, you might be writing trace logs to one file but when an error is encountered, write to the same file, then into error file and send an email at the same time… 38 | 39 |

40 | 41 | 42 | 43 |

44 | -------------------------------------------------------------------------------- /sections/projectstructre/breakintcomponents.md: -------------------------------------------------------------------------------- 1 | # Structure your solution by components 2 | 3 |

4 | 5 | 6 | ### One Paragraph Explainer 7 | 8 | For medium sized apps and above, monoliths are really bad - having one big software with many dependencies is just hard to reason about and often leads to spaghetti code. Even smart architects — those who are skilled enough to tame the beast and 'modularize' it — spend great mental effort on design, and each change requires carefully evaluating the impact on other dependant objects. The ultimate solution is to develop small software: divide the whole stack into self-contained components that don't share files with others, each constitutes very few files (e.g. API, service, data access, test, etc.) so that it's very easy to reason about it. Some may call this 'microservices' architecture — it's important to understand that microservices is not a spec which you must follow, but rather a set of principles. You may adopt many principles into a full-blown microservices architecture or adopt only few. Both are good as long as you keep the software complexity low. The very least you should do is create basic borders between components, assign a folder in your project root for each business component and make it self contained - other components are allowed to consume its functionality only through its public interface or API. This is the foundation for keeping your components simple, avoid dependency hell and pave the way to full-blown microservices in the future once your app grows. 9 | 10 |

11 | 12 | 13 | ### Blog Quote: "Scaling requires scaling of the entire application" 14 | From the blog MartinFowler.com 15 | 16 | > Monolithic applications can be successful, but increasingly people are feeling frustrations with them - especially as more applications are being deployed to the cloud. Change cycles are tied together - a change made to a small part of the application, requires the entire monolith to be rebuilt and deployed. Over time it's often hard to keep a good modular structure, making it harder to keep changes that ought to only affect one module within that module. Scaling requires scaling of the entire application rather than parts of it that require greater resource. 17 | 18 |

19 | 20 | ### Blog Quote: "So what does the architecture of your application scream?" 21 | From the blog [uncle-bob](https://8thlight.com/blog/uncle-bob/2011/09/30/Screaming-Architecture.html) 22 | 23 | > ...if you were looking at the architecture of a library, you’d likely see a grand entrance, an area for check-in-out clerks, reading areas, small conference rooms, and gallery after gallery capable of holding bookshelves for all the books in the library. That architecture would scream: Library.
24 | So what does the architecture of your application scream? When you look at the top level directory structure, and the source files in the highest level package; do they scream: Health Care System, or Accounting System, or Inventory Management System? Or do they scream: Rails, or Spring/Hibernate, or ASP?. 25 | 26 | 27 | 28 |

29 | 30 | ### Good: Structure your solution by self-contained components 31 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/structurebycomponents.PNG "Structuring solution by components") 32 | 33 |

34 | 35 | ### Bad: Group your files by technical role 36 | ![alt text](https://github.com/i0natan/nodebestpractices/blob/master/assets/images/structurebyroles.PNG "Structuring solution by technical roles") 37 | -------------------------------------------------------------------------------- /sections/errorhandling/asyncerrorhandling.md: -------------------------------------------------------------------------------- 1 | # Use Async-Await or promises for async error handling 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | Callbacks don’t scale as they are not familiar to most programmers, force to check errors all over, deal with nasty code nesting and make it difficult to reason about the code flow. Promise libraries like BlueBird, async, and Q pack a standard code style using RETURN and THROW to control the program flow. Specifically, they support the favorite try-catch error handling style which allows freeing the main code path from dealing with errors in every function 7 | 8 | 9 | ### Code Example – using promises to catch errors 10 | 11 | 12 | ```javascript 13 | doWork() 14 | .then(doWork) 15 | .then(doOtherWork) 16 | .then((result) => doWork) 17 | .catch((error) => {throw error;}) 18 | .then(verify); 19 | ``` 20 | 21 | ### Anti pattern code example – callback style error handling 22 | 23 | ```javascript 24 | getData(someParameter, function(err, result){ 25 | if(err != null) 26 | // do something like calling the given callback function and pass the error 27 | getMoreData(a, function(err, result){ 28 | if(err != null) 29 | // do something like calling the given callback function and pass the error 30 | getMoreData(b, function(c){ 31 | getMoreData(d, function(e){ 32 | if(err != null) 33 | // you get the idea?  34 | }); 35 | }); 36 | ``` 37 | 38 | ### Blog Quote: "We have a problem with promises" 39 | From the blog pouchdb.com 40 | 41 | > ……And in fact, callbacks do something even more sinister: they deprive us of the stack, which is something we usually take for granted in programming languages. Writing code without a stack is a lot like driving a car without a brake pedal: you don’t realize how badly you need it, until you reach for it and it’s not there. The whole point of promises is to give us back the language fundamentals we lost when we went async: return, throw, and the stack. But you have to know how to use promises correctly in order to take advantage of them. 42 | 43 | ### Blog Quote: "The promises method is much more compact" 44 | From the blog gosquared.com 45 | 46 | > ………The promises method is much more compact, clearer and quicker to write. If an error or exception occurs within any of the ops it is handled by the single .catch() handler. Having this single place to handle all errors means you don’t need to write error checking for each stage of the work. 47 | 48 | ### Blog Quote: "Promises are native ES6, can be used with generators" 49 | From the blog StrongLoop 50 | 51 | > ….Callbacks have a lousy error-handling story. Promises are better. Marry the built-in error handling in Express with promises and significantly lower the chances of an uncaught exception. Promises are native ES6, can be used with generators, and ES7 proposals like async/await through compilers like Babel 52 | 53 | ### Blog Quote: "All those regular flow control constructs you are used to are completely broken" 54 | From the blog Benno’s 55 | 56 | > ……One of the best things about asynchronous, callback based programming is that basically all those regular flow control constructs you are used to are completely broken. However, the one I find most broken is the handling of exceptions. Javascript provides a fairly familiar try…catch construct for dealing with exceptions. The problems with exceptions is that they provide a great way of short-cutting errors up a call stack, but end up being completely useless of the error happens on a different stack… 57 | -------------------------------------------------------------------------------- /sections/errorhandling/operationalvsprogrammererror.md: -------------------------------------------------------------------------------- 1 | # Distinguish operational vs programmer errors 2 | 3 | ### One Paragraph Explainer 4 | 5 | Distinguishing the following two error types will minimize your app downtime and helps avoid crazy bugs: Operational errors refer to situations where you understand what happened and the impact of it – for example, a query to some HTTP service failed due to connection problem. On the other hand, programmer errors refer to cases where you have no idea why and sometimes where an error came from – it might be some code that tried to read an undefined value or DB connection pool that leaks memory. Operational errors are relatively easy to handle – usually logging the error is enough. Things become hairy when a programmer error pops up, the application might be in an inconsistent state and there’s nothing better you can do than to restart gracefully 6 | 7 | 8 | 9 | ### Code Example – marking an error as operational (trusted) 10 | 11 | ```javascript 12 | // marking an error object as operational 13 | var myError = new Error("How can I add new product when no value provided?"); 14 | myError.isOperational = true; 15 | 16 | // or if you're using some centralized error factory (see other examples at the bullet "Use only the built-in Error object") 17 | function appError(commonType, description, isOperational) { 18 | Error.call(this); 19 | Error.captureStackTrace(this); 20 | this.commonType = commonType; 21 | this.description = description; 22 | this.isOperational = isOperational; 23 | }; 24 | 25 | throw new appError(errorManagement.commonErrors.InvalidInput, "Describe here what happened", true); 26 | 27 | ``` 28 | 29 | ### Blog Quote: "Programmer errors are bugs in the program" 30 | From the blog Joyent, ranked 1 for the keywords “Node.JS error handling” 31 | 32 | > …The best way to recover from programmer errors is to crash immediately. You should run your programs using a restarter that will automatically restart the program in the event of a crash. With a restarter in place, crashing is the fastest way to restore reliable service in the face of a transient programmer error… 33 | 34 | ### Blog Quote: "No safe way to leave without creating some undefined brittle state" 35 | From Node.JS official documentation 36 | 37 | > …By the very nature of how throw works in JavaScript, there is almost never any way to safely “pick up where you left off”, without leaking references, or creating some other sort of undefined brittle state. The safest way to respond to a thrown error is to shut down the process. Of course, in a normal web server, you might have many connections open, and it is not reasonable to abruptly shut those down because an error was triggered by someone else. The better approach is to send an error response to the request that triggered the error, while letting the others finish in their normal time, and stop listening for new requests in that worker. 38 | 39 | 40 | ### Blog Quote: "Otherwise you risk the state of your application" 41 | From the blog debugable.com, ranked 3 for the keywords “Node.JS uncaught exception” 42 | 43 | > …So, unless you really know what you are doing, you should perform a graceful restart of your service after receiving an “uncaughtException” exception event. Otherwise you risk the state of your application, or that of 3rd party libraries to become inconsistent, leading to all kinds of crazy bugs… 44 | 45 | ### Blog Quote: "Blog Quote: There are three schools of thoughts on error handling" 46 | From the blog: JS Recipes 47 | 48 | > …There are primarily three schools of thoughts on error handling: 49 | 1. Let the application crash and restart it. 50 | 2. Handle all possible errors and never crash. 51 | 3. Balanced approach between the two 52 | -------------------------------------------------------------------------------- /sections/drafts/readme-general-toc-2.md: -------------------------------------------------------------------------------- 1 | # Node.JS Best Practices 2 | 3 | 53 items Last update: 7 days ago Updated for Node v.8.4 4 | 5 | ![Node.js Best Practices](assets/images/banner-1.png) 6 | 7 | # Welcome to Node.js Best Practices 8 | 9 | Welcome to the biggest compilation of Node.JS best practices. The content below was gathered from all top ranked books and posts and is updated constantly - when you read here rest assure that no significant tip slipped away. Feel at home - we love to discuss via PRs, issues or Gitter. 10 | 11 | ## Table of Contents 12 | * [Project Setup Practices (18)](#project-setup-practices) 13 | * [Code Style Practices (11) ](#code-style-practices) 14 | * [Error Handling Practices (14) ](#error-handling-practices) 15 | * [Going To Production Practices (21) ](#going-to-production-practices) 16 | * [Testing Practices (9) ](#deployment-practices) 17 | * [Security Practices (8) ](#security-practices) 18 | 19 |

20 | # `Project Setup Practices` 21 | 22 | ## ✔ 1. Structure your solution by feature ('microservices') 23 | 24 | **TL&DR:** The worst large applications pitfal is a huge code base where hundreds of dependencies slow down developers as try to incorporate new features. Partioning into small units ensures that each unit is kept simple and very easy to maintain. This strategy pushes the complexity to the higher level - designing the cross-component interactions. 25 | 26 | **Otherwise:** Developing a new feature with a change to few objects demands to evaluate how this changes might affect dozends of dependants and ach deployment becomes a fear. 27 | 28 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 29 | 30 |
31 | 32 | ## ✔ 2. Layer your app, keep Express within its boundaries 33 | 34 | **TL&DR:** It's very common to see Express API passes the express objects (req, res) to business logic and data layers, sometimes even to every function - this makes your application depedant on and accessible by Express only. What if your code should be reached by testing console or CRON job? instead create your own context object with cross-cutting-concern properties like the user roles and inject into other layers, or use 'thread-level variables' libraries like continuation local storage 35 | 36 | **Otherwise:** Application can be accessed by Express only and require to create complex testing mocks 37 | 38 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 39 | 40 |

41 | 42 | ## ✔ 3. Configure ESLint with node-specific plugins 43 | 44 | **TL&DR:** Monitoring is a game of finding out issues before our customers do – obviously this should be assigned unprecedented importance. The market is overwhelmed with offers thus consider starting with defining the basic metrics you must follow (my sug 45 | 46 | **Otherwise:** You end-up with a blackbox that is hard to reason about, then you start re-writing all logging statements to add additional information 47 | 48 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 49 | 50 |


51 | # `Code Style Practices` 52 | 53 | 54 |


55 | # `Error Handling Practices` 56 |

⬆ Return to top

57 | 58 | ## ✔ Use async-await for async error handling 59 | 60 | **TL;DR:** Handling async errors in callback style is probably the fastest way to hell (a.k.a the pyramid of doom). The best gift you can give to your code is using instead a reputable promise library or async-await which provides much compact and familiar code syntax like try-catch 61 | 62 | **Otherwise:** Node.JS callback style, function(err, response), is a promising way to un-maintainable code due to the mix of error handling with casual code, excessive nesting and awkward coding patterns 63 | 64 | 🔗 [**Use async-await for async error handling**](/sections/errorhandling/asyncawait.md) 65 | 66 | 67 | 68 |


69 | # `Going To Production Practices` 70 | 71 | 72 |


73 | # `Deployment Practices` 74 | 75 | 76 |


77 | # `Security Practices` 78 | 79 | -------------------------------------------------------------------------------- /sections/errorhandling/centralizedhandling.md: -------------------------------------------------------------------------------- 1 | # Handle errors centrally, through but not within middleware 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | Without one dedicated object for error handling, greater are the chances of important errors hiding under the radar due to improper handling. The error handler object is responsible for making the error visible, for example by writing to a well-formatted logger, sending events to some monitoring product or to an admin directly via email. A typical error handling flow might be: Some module throws an error -> API router catches the error -> it propagates the error to the middleware (e.g. Express, KOA) who is responsible for catching errors -> a centralized error handler is called -> the middleware is being told whether this error is an untrusted error (not operational) so it can restart the app gracefully. Note that it’s a common, yet wrong, practice to handle errors within Express middleware – doing so will not cover errors that are thrown in non-web interfaces 7 | 8 | 9 | 10 | ### Code Example – a typical error flow 11 | 12 | ```javascript 13 | // DAL layer, we don't handle errors here 14 | DB.addDocument(newCustomer, (error, result) => { 15 | if (error) 16 | throw new Error("Great error explanation comes here", other useful parameters) 17 | }); 18 | 19 | // API route code, we catch both sync and async errors and forward to the middleware 20 | try { 21 | customerService.addNew(req.body).then((result) => { 22 | res.status(200).json(result); 23 | }).catch((error) => { 24 | next(error) 25 | }); 26 | } 27 | catch (error) { 28 | next(error); 29 | } 30 | 31 | // Error handling middleware, we delegate the handling to the centralized error handler 32 | app.use((err, req, res, next) => { 33 | errorHandler.handleError(err).then((isOperationalError) => { 34 | if (!isOperationalError) 35 | next(err); 36 | }); 37 | }); 38 | 39 | ``` 40 | 41 | ### Code example – handling errors within a dedicated object 42 | 43 | ```javascript 44 | module.exports.handler = new errorHandler(); 45 | 46 | function errorHandler(){ 47 | this.handleError = function (error) { 48 | return logger.logError(err).then(sendMailToAdminIfCritical).then(saveInOpsQueueIfCritical).then(determineIfOperationalError); 49 | } 50 | } 51 | ``` 52 | 53 | ### Code Example – Anti Pattern: handling errors within the middleware 54 | 55 | ```javascript 56 | // middleware handling the error directly, who will handle Cron jobs and testing errors? 57 | app.use((err, req, res, next) => { 58 | logger.logError(err); 59 | if(err.severity == errors.high) 60 | mailer.sendMail(configuration.adminMail, "Critical error occured", err); 61 | if(!err.isOperational) 62 | next(err); 63 | }); 64 | 65 | ``` 66 | 67 | ### Blog Quote: "Sometimes lower levels can’t do anything useful except propagate the error to their caller" 68 | From the blog Joyent, ranked 1 for the keywords “Node.JS error handling” 69 | 70 | > …You may end up handling the same error at several levels of the stack. This happens when lower levels can’t do anything useful except propagate the error to their caller, which propagates the error to its caller, and so on. Often, only the top-level caller knows what the appropriate response is, whether that’s to retry the operation, report an error to the user, or something else. But that doesn’t mean you should try to report all errors to a single top-level callback, because that callback itself can’t know in what context the error occurred… 71 | 72 | 73 | ### Blog Quote: "Handling each err individually would result in tremendous duplication" 74 | From the blog JS Recipes, ranked 17 for the keywords “Node.JS error handling” 75 | 76 | > ……In Hackathon Starter api.js controller alone, there are over 79 occurences of error objects. Handling each err individually would result in tremendous amount of code duplication. The next best thing you can do is to delegate all error handling logic to an Express middleware… 77 | 78 | 79 | ### Blog Quote: "HTTP errors have no place in your database code" 80 | From the blog Daily JS, ranked 14 for the keywords “Node.JS error handling” 81 | 82 | > ……You should set useful properties in error objects, but use such properties consistently. And, don’t cross the streams: HTTP errors have no place in your database code. Or for browser developers, Ajax errors have a place in code that talks to the server, but not code that processes Mustache templates… 83 | 84 | -------------------------------------------------------------------------------- /sections/drafts/readme-general-toc-1.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | Node.js Best Practices 5 |

6 | 7 | 53 items Last update: 7 days ago Updated for Node v.8.4 8 | 9 | 10 | 11 | # Welcome to Node.js Best Practices 12 | 13 | Welcome to the biggest compilation of Node.JS best practices. The content below was gathered from all top ranked books and posts and is updated constantly - when you read here rest assure that no significant tip slipped away. Feel at home - we love to discuss via PRs, issues or Gitter. 14 | 15 | ## Table of Contents 16 | * [Project Setup Practices (18)](#project-setup-practices) 17 | * [Code Style Practices (11) ](#code-style-practices) 18 | * [Error Handling Practices (14) ](#error-handling-practices) 19 | * [Going To Production Practices (21) ](#going-to-production-practices) 20 | * [Testing Practices (9) ](#deployment-practices) 21 | * [Security Practices (8) ](#security-practices) 22 | 23 | 24 |

25 | # `Project Setup Practices` 26 | 27 | ## ✔ 1. Structure your solution by feature ('microservices') 28 | 29 | **TL&DR:** The worst large applications pitfal is a huge code base with hundreds of dependencies that slow down they developers as they try to incorporate new features. Partioning into small units ensures that each unit is kept simple and easy to maintain. This strategy pushes the complexity to the higher level - designing the cross-component interactions. 30 | 31 | **Otherwise:** Developing a new feature with a change to few objects demands to evaluate how this changes might affect dozends of dependants and ach deployment becomes a fear. 32 | 33 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 34 | 35 |

36 | 37 | ## ✔ 2. Layer your app, keep Express within its boundaries 38 | 39 | **TL&DR:** It's very common to see Express API passes the express objects (req, res) to business logic and data layers, sometimes even to every function - this makes your application depedant on and accessible by Express only. What if your code should be reached by testing console or CRON job? instead create your own context object with cross-cutting-concern properties like the user roles and inject into other layers, or use 'thread-level variables' libraries like continuation local storage 40 | 41 | **Otherwise:** Application can be accessed by Express only and require to create complex testing mocks 42 | 43 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 44 | 45 |

46 | 47 | ## ✔ 3. Configure ESLint with node-specific plugins 48 | 49 | **TL&DR:** Monitoring is a game of finding out issues before our customers do – obviously this should be assigned unprecedented importance. The market is overwhelmed with offers thus consider starting with defining the basic metrics you must follow (my sug 50 | 51 | **Otherwise:** You end-up with a blackbox that is hard to reason about, then you start re-writing all logging statements to add additional information 52 | 53 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 54 | 55 | 56 |


57 | # `Code Style Practices` 58 | 59 | 60 |


61 | # `Error Handling Practices` 62 |

⬆ Return to top

63 | 64 | ## ✔ Use async-await for async error handling 65 | 66 | * **TL;DR:** Handling async errors in callback style is probably the fastest way to hell (a.k.a the pyramid of doom). The best gift you can give to your code is using instead a reputable promise library or async-await which provides much compact and familiar code syntax like try-catch 67 | 68 | * **Otherwise:** Node.JS callback style, function(err, response), is a promising way to un-maintainable code due to the mix of error handling with casual code, excessive nesting and awkward coding patterns 69 | 70 | 🔗 [**Use async-await for async error handling**](/sections/errorhandling/asyncawait.md) 71 | 72 | 73 | 74 |


75 | # `Going To Production Practices` 76 | 77 | 78 |


79 | # `Deployment Practices` 80 | 81 | 82 |


83 | # `Security Practices` 84 | 85 | -------------------------------------------------------------------------------- /sections/drafts/readme-general-toc-3.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | Node.js Best Practices 5 |

6 | 7 | 53 items Last update: 7 days ago Updated for Node v.8.4 8 | 9 | 10 | 11 | # Welcome to Node.js Best Practices 12 | 13 | Welcome to the biggest compilation of Node.JS best practices, based on our check it's also the largest collection on any programming language (more than 53 items). The content below was gathered from all top ranked books and posts and is updated constantly - if you read here you can rest assure that no significant tip slipped away. Feel at home - we love to discuss via PRs, issues or Gitter. 14 |

15 | ## Table of Contents 16 | * [Project Setup Practices (18)](#project-setup-practices) 17 | * [Code Style Practices (11) ](#code-style-practices) 18 | * [Error Handling Practices (14) ](#error-handling-practices) 19 | * [Going To Production Practices (21) ](#going-to-production-practices) 20 | * [Testing Practices (9) ](#deployment-practices) 21 | * [Security Practices (8) ](#security-practices) 22 | 23 |

24 | # `Project Setup Practices` 25 | 26 | ## ✔ 1. Structure your solution by feature ('microservices') 27 | 28 | **TL&DR:** The worst large applications pitfal is a huge code base where hundreds of dependencies slow down developers as try to incorporate new features. Partioning into small units ensures that each unit is kept simple and very easy to maintain. This strategy pushes the complexity to the higher level - designing the cross-component interactions. 29 | 30 | **Otherwise:** Developing a new feature with a change to few objects demands to evaluate how this changes might affect dozends of dependants and ach deployment becomes a fear. 31 | 32 |

33 | 34 | ## ✔ 2. Layer your app, keep Express within its boundaries 35 | 36 | **TL&DR:** It's very common to see Express API passes the express objects (req, res) to business logic and data layers, sometimes even to every function - this makes your application depedant on and accessible by Express only. What if your code should be reached by testing console or CRON job? instead create your own context object with cross-cutting-concern properties like the user roles and inject into other layers, or use 'thread-level variables' libraries like continuation local storage 37 | 38 | **Otherwise:** Application can be accessed by Express only and require to create complex testing mocks 39 | 40 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 41 | 42 | 43 |

44 | 45 | ## ✔ 3. Configure ESLint with node-specific plugins 46 | 47 | **TL&DR:** Monitoring is a game of finding out issues before our customers do – obviously this should be assigned unprecedented importance. The market is overwhelmed with offers thus consider starting with defining the basic metrics you must follow (my sug 48 | 49 | **Otherwise:** You end-up with a blackbox that is hard to reason about, then you start re-writing all logging statements to add additional information 50 | 51 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 52 | 53 | 54 |


55 | # `Code Style Practices` 56 | 57 | 58 |


59 | # `Error Handling Practices` 60 |

⬆ Return to top

61 | 62 | ## ✔ Use async-await for async error handling 63 | 64 | **TL;DR:** Handling async errors in callback style is probably the fastest way to hell (a.k.a the pyramid of doom). The best gift you can give to your code is using instead a reputable promise library or async-await which provides much compact and familiar code syntax like try-catch 65 | 66 | **Otherwise:** Node.JS callback style, function(err, response), is a promising way to un-maintainable code due to the mix of error handling with casual code, excessive nesting and awkward coding patterns 67 | 68 | 🔗 [**Use async-await for async error handling**](/sections/errorhandling/asyncawait.md) 69 | 70 | 71 | 72 |


73 | # `Going To Production Practices` 74 | 75 | 76 |


77 | # `Deployment Practices` 78 | 79 | 80 |


81 | # `Security Practices` 82 | 83 | -------------------------------------------------------------------------------- /sections/errorhandling/useonlythebuiltinerror.md: -------------------------------------------------------------------------------- 1 | # Use only the built-in Error object 2 | 3 | 4 | ### One Paragraph Explainer 5 | 6 | The permissive nature of JS along with its variety code-flow options (e.g. EventEmitter, Callbacks, Promises, etc) pushes to great variance in how developers raise errors – some use strings, other define their own custom types. Using Node.JS built-in Error object helps to keep uniformity within your code and with 3rd party libraries, it also preserves significant information like the StackTrace. When raising the exception, it’s usually a good practice to fill it with additional contextual properties like the error name and the associated HTTP error code. To achieve this uniformity and practices, consider extending the Error object with additional properties, see code example below 7 | Blog Quote: “I don’t see the value in having lots of different types” 8 | From the blog Ben Nadel, ranked 5 for the keywords “Node.JS error object” 9 | …”Personally, I don’t see the value in having lots of different types of error objects – JavaScript, as a language, doesn’t seem to cater to Constructor-based error-catching. As such, differentiating on an object property seems far easier than differentiating on a Constructor type… 10 | 11 | 12 | 13 | ### Code Example – doing it right 14 | 15 | ```javascript 16 | // throwing an Error from typical function, whether sync or async 17 | if(!productToAdd) 18 | throw new Error("How can I add new product when no value provided?"); 19 | 20 | // 'throwing' an Error from EventEmitter 21 | const myEmitter = new MyEmitter(); 22 | myEmitter.emit('error', new Error('whoops!')); 23 | 24 | // 'throwing' an Error from a Promise 25 | return new Promise(function (resolve, reject) { 26 | Return DAL.getProduct(productToAdd.id).then((existingProduct) =>{ 27 | if(existingProduct != null) 28 | reject(new Error("Why fooling us and trying to add an existing product?")); 29 | }) 30 | }); 31 | ``` 32 | 33 | ### Code example – Anti Pattern 34 | 35 | ```javascript 36 | // throwing a string lacks any stack trace information and other important data properties 37 | if(!productToAdd) 38 | throw ("How can I add new product when no value provided?"); 39 | 40 | ``` 41 | 42 | ### Code example – doing it even better 43 | 44 | ```javascript 45 | // centralized error object that derives from Node’s Error 46 | function appError(name, httpCode, description, isOperational) { 47 | Error.call(this); 48 | Error.captureStackTrace(this); 49 | this.name = name; 50 | //...other properties assigned here 51 | }; 52 | 53 | appError.prototype.__proto__ = Error.prototype; 54 | 55 | module.exports.appError = appError; 56 | 57 | // client throwing an exception 58 | if(user == null) 59 | throw new appError(commonErrors.resourceNotFound, commonHTTPErrors.notFound, "further explanation", true) 60 | ``` 61 | 62 | 63 | ### Blog Quote: "A string is not an error" 64 | From the blog devthought.com, ranked 6 for the keywords “Node.JS error object” 65 | 66 | > …passing a string instead of an error results in reduced interoperability between modules. It breaks contracts with APIs that might be performing instanceof Error checks, or that want to know more about the error. Error objects, as we’ll see, have very interesting properties in modern JavaScript engines besides holding the message passed to the constructor… 67 | Blog Quote: “All JavaScript and System errors raised by Node.js inherit from Error” 68 | 69 | ### Blog Quote: "Inheriting from Error doesn’t add too much value" 70 | From the blog machadogj 71 | 72 | > …One problem that I have with the Error class is that is not so simple to extend. Of course you can inherit the class and create your own Error classes like HttpError, DbError, etc. However that takes time, and doesn’t add too much value unless you are doing something with types. Sometimes, you just want to add a message, and keep the inner error, and sometimes you might want to extend the error with parameters, and such… 73 | 74 | ### Blog Quote: "All JavaScript and System errors raised by Node.js inherit from Error" 75 | From Node.JS official documentation 76 | 77 | > …All JavaScript and System errors raised by Node.js inherit from, or are instances of, the standard JavaScript Error class and are guaranteed to provide at least the properties available on that class. A generic JavaScript Error object that does not denote any specific circumstance of why the error occurred. Error objects capture a “stack trace” detailing the point in the code at which the Error was instantiated, and may provide a text description of the error.All errors generated by Node.js, including all System and JavaScript errors, will either be instances of, or inherit from, the Error class… 78 | -------------------------------------------------------------------------------- /sections/drafts/readme-general-toc-4.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | Node.js Best Practices 5 |

6 | 7 | 53 items Last update: 7 days ago Updated for Node v.8.4 8 | 9 | 10 | 11 | # Welcome to Node.js Best Practices 12 | 13 | Welcome to the biggest compilation of Node.JS best practices, based on our check it's also the largest collection on any programming language (more than 53 items). The content below was gathered from all top ranked books and posts and is updated constantly - if you read here you can rest assure that no significant tip slipped away. Feel at home - we love to discuss via PRs, issues or Gitter. 14 | 15 | ## Table of Contents 16 | * [Project Setup Practices (18)](#project-setup-practices) 17 | * [Code Style Practices (11) ](#code-style-practices) 18 | * [Error Handling Practices (14) ](#error-handling-practices) 19 | * [Going To Production Practices (21) ](#going-to-production-practices) 20 | * [Testing Practices (9) ](#deployment-practices) 21 | * [Security Practices (8) ](#security-practices) 22 | 23 |

24 | # `Project Setup Practices` 25 | 26 | ## ![](assets/images/checkbox-sm.png) 1. Structure your solution by feature ('microservices') 27 | 28 | **TL&DR:** The worst large applications pitfal is a huge code base where hundreds of dependencies slow down developers as try to incorporate new features. Partioning into small units ensures that each unit is kept simple and very easy to maintain. This strategy pushes the complexity to the higher level - designing the cross-component interactions. 29 | 30 | **Otherwise:** Developing a new feature with a change to few objects demands to evaluate how this changes might affect dozends of dependants and ach deployment becomes a fear. 31 | 32 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 33 | 34 |

35 | 36 | ## ![](assets/images/checkbox-sm.png) 2. Layer your app, keep Express within its boundaries 37 | 38 | **TL&DR:** It's very common to see Express API passes the express objects (req, res) to business logic and data layers, sometimes even to every function - this makes your application depedant on and accessible by Express only. What if your code should be reached by testing console or CRON job? instead create your own context object with cross-cutting-concern properties like the user roles and inject into other layers, or use 'thread-level variables' libraries like continuation local storage 39 | 40 | **Otherwise:** Application can be accesses by Express only and require to create complex testing mocks 41 | 42 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 43 | 44 |

45 | 46 | ## ![](assets/images/checkbox-sm.png) 3. Configure ESLint with node-specific plugins 47 | 48 | **TL&DR:** Monitoring is a game of finding out issues before our customers do – obviously this should be assigned unprecedented importance. The market is overwhelmed with offers thus consider starting with defining the basic metrics you must follow (my sug 49 | 50 | **Otherwise:** You end-up with a blackbox that is hard to reason about, then you start re-writing all logging statements to add additional information 51 | 52 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 53 | 54 | 55 | 56 |

57 | 58 | ## Additional 15 bullets will appear here 59 | 60 |


61 | # `Code Style Practices` 62 | 63 | ## ![](assets/images/checkbox-sm.png) 1. Use async-await 64 | 65 | **TL&DR:** Monitoring is a game of finding out issues before our customers do – obviously this should be assigned unprecedented importance. The market is overwhelmed with offers thus consider starting with defining the basic metrics you must follow (my sug 66 | 67 | **Otherwise:** You end-up with a blackbox that is hard to reason about, then you start re-writing all logging statements to add additional information 68 | 69 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 70 | 71 |

72 | 73 | ## ![](assets/images/checkbox-sm.png) 2. Break into small classes or objects 74 | 75 | **TL&DR:** Monitoring is a game of finding out issues before our customers do – obviously this should be assigned unprecedented importance. The market is overwhelmed with offers thus consider starting with defining the basic metrics you must follow (my sug 76 | 77 | **Otherwise:** You end-up with a blackbox that is hard to reason about, then you start re-writing all logging statements to add additional information 78 | 79 | 🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md) 80 | 81 |


82 | # `Error Handling Practices` 83 |

⬆ Return to top

84 | 85 | ## Use async-await for async error handling 86 | 87 | **TL;DR:** Handling async errors in callback style is probably the fastest way to hell (a.k.a the pyramid of doom). The best gift you can give to your code is using instead a reputable promise library or async-await which provides much compact and familiar code syntax like try-catch 88 | 89 | **Otherwise:** Node.JS callback style, function(err, response), is a promising way to un-maintainable code due to the mix of error handling with casual code, excessive nesting and awkward coding patterns 90 | 91 | 🔗 [**Use async-await for async error handling**](/sections/errorhandling/asyncawait.md) 92 | 93 |

94 | 95 | ## Use async-await for async error handling 96 | 97 | **TL;DR:** Handling async errors in callback style is probably the fastest way to hell (a.k.a the pyramid of doom). The best gift you can give to your code is using instead a reputable promise library or async-await which provides much compact and familiar code syntax like try-catch 98 | 99 | **Otherwise:** Node.JS callback style, function(err, response), is a promising way to un-maintainable code due to the mix of error handling with casual code, excessive nesting and awkward coding patterns 100 | 101 | 🔗 [**Use async-await for async error handling**](/sections/errorhandling/asyncawait.md) 102 | 103 | 104 | 105 |


106 | # `Going To Production Practices` 107 | 108 | 109 |


110 | # `Deployment Practices` 111 | 112 | 113 |


114 | # `Security Practices` 115 | 116 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ## creative commons 2 | 3 | # Attribution-ShareAlike 4.0 International 4 | 5 | Creative Commons Corporation (“Creative Commons”) is not a law firm and does not provide legal services or legal advice. Distribution of Creative Commons public licenses does not create a lawyer-client or other relationship. Creative Commons makes its licenses and related information available on an “as-is” basis. Creative Commons gives no warranties regarding its licenses, any material licensed under their terms and conditions, or any related information. Creative Commons disclaims all liability for damages resulting from their use to the fullest extent possible. 6 | 7 | ### Using Creative Commons Public Licenses 8 | 9 | Creative Commons public licenses provide a standard set of terms and conditions that creators and other rights holders may use to share original works of authorship and other material subject to copyright and certain other rights specified in the public license below. The following considerations are for informational purposes only, are not exhaustive, and do not form part of our licenses. 10 | 11 | * __Considerations for licensors:__ Our public licenses are intended for use by those authorized to give the public permission to use material in ways otherwise restricted by copyright and certain other rights. Our licenses are irrevocable. Licensors should read and understand the terms and conditions of the license they choose before applying it. Licensors should also secure all rights necessary before applying our licenses so that the public can reuse the material as expected. Licensors should clearly mark any material not subject to the license. This includes other CC-licensed material, or material used under an exception or limitation to copyright. [More considerations for licensors](http://wiki.creativecommons.org/Considerations_for_licensors_and_licensees#Considerations_for_licensors). 12 | 13 | * __Considerations for the public:__ By using one of our public licenses, a licensor grants the public permission to use the licensed material under specified terms and conditions. If the licensor’s permission is not necessary for any reason–for example, because of any applicable exception or limitation to copyright–then that use is not regulated by the license. Our licenses grant only permissions under copyright and certain other rights that a licensor has authority to grant. Use of the licensed material may still be restricted for other reasons, including because others have copyright or other rights in the material. A licensor may make special requests, such as asking that all changes be marked or described. Although not required by our licenses, you are encouraged to respect those requests where reasonable. [More considerations for the public](http://wiki.creativecommons.org/Considerations_for_licensors_and_licensees#Considerations_for_licensees). 14 | 15 | ## Creative Commons Attribution-ShareAlike 4.0 International Public License 16 | 17 | By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. 18 | 19 | ### Section 1 – Definitions. 20 | 21 | a. __Adapted Material__ means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. 22 | 23 | b. __Adapter's License__ means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. 24 | 25 | c. __BY-SA Compatible License__ means a license listed at [creativecommons.org/compatiblelicenses](http://creativecommons.org/compatiblelicenses), approved by Creative Commons as essentially the equivalent of this Public License. 26 | 27 | d. __Copyright and Similar Rights__ means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. 28 | 29 | e. __Effective Technological Measures__ means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. 30 | 31 | f. __Exceptions and Limitations__ means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. 32 | 33 | g. __License Elements__ means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution and ShareAlike. 34 | 35 | h. __Licensed Material__ means the artistic or literary work, database, or other material to which the Licensor applied this Public License. 36 | 37 | i. __Licensed Rights__ means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. 38 | 39 | j. __Licensor__ means the individual(s) or entity(ies) granting rights under this Public License. 40 | 41 | k. __Share__ means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. 42 | 43 | l. __Sui Generis Database Rights__ means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. 44 | 45 | m. __You__ means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. 46 | 47 | ### Section 2 – Scope. 48 | 49 | a. ___License grant.___ 50 | 51 | 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: 52 | 53 | A. reproduce and Share the Licensed Material, in whole or in part; and 54 | 55 | B. produce, reproduce, and Share Adapted Material. 56 | 57 | 2. __Exceptions and Limitations.__ For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. 58 | 59 | 3. __Term.__ The term of this Public License is specified in Section 6(a). 60 | 61 | 4. __Media and formats; technical modifications allowed.__ The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. 62 | 63 | 5. __Downstream recipients.__ 64 | 65 | A. __Offer from the Licensor – Licensed Material.__ Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. 66 | 67 | B. __Additional offer from the Licensor – Adapted Material. Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter’s License You apply. 68 | 69 | C. __No downstream restrictions.__ You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. 70 | 71 | 6. __No endorsement.__ Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). 72 | 73 | b. ___Other rights.___ 74 | 75 | 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. 76 | 77 | 2. Patent and trademark rights are not licensed under this Public License. 78 | 79 | 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties. 80 | 81 | ### Section 3 – License Conditions. 82 | 83 | Your exercise of the Licensed Rights is expressly made subject to the following conditions. 84 | 85 | a. ___Attribution.___ 86 | 87 | 1. If You Share the Licensed Material (including in modified form), You must: 88 | 89 | A. retain the following if it is supplied by the Licensor with the Licensed Material: 90 | 91 | i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); 92 | 93 | ii. a copyright notice; 94 | 95 | iii. a notice that refers to this Public License; 96 | 97 | iv. a notice that refers to the disclaimer of warranties; 98 | 99 | v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; 100 | 101 | B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and 102 | 103 | C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. 104 | 105 | 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. 106 | 107 | 3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. 108 | 109 | b. ___ShareAlike.___ 110 | 111 | In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. 112 | 113 | 1. The Adapter’s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-SA Compatible License. 114 | 115 | 2. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. 116 | 117 | 3. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. 118 | 119 | ### Section 4 – Sui Generis Database Rights. 120 | 121 | Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: 122 | 123 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database; 124 | 125 | b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and 126 | 127 | c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. 128 | 129 | For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. 130 | 131 | ### Section 5 – Disclaimer of Warranties and Limitation of Liability. 132 | 133 | a. __Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.__ 134 | 135 | b. __To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.__ 136 | 137 | c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. 138 | 139 | ### Section 6 – Term and Termination. 140 | 141 | a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. 142 | 143 | b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: 144 | 145 | 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or 146 | 147 | 2. upon express reinstatement by the Licensor. 148 | 149 | For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. 150 | 151 | c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. 152 | 153 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. 154 | 155 | ### Section 7 – Other Terms and Conditions. 156 | 157 | a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. 158 | 159 | b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License.t stated herein are separate from and independent of the terms and conditions of this Public License. 160 | 161 | ### Section 8 – Interpretation. 162 | 163 | a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. 164 | 165 | b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. 166 | 167 | c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. 168 | 169 | d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. 170 | 171 | > Creative Commons is not a party to its public licenses. Notwithstanding, Creative Commons may elect to apply one of its public licenses to material it publishes and in those instances will be considered the “Licensor.” Except for the limited purpose of indicating that material is shared under a Creative Commons public license or as otherwise permitted by the Creative Commons policies published at [creativecommons.org/policies](http://creativecommons.org/policies), Creative Commons does not authorize the use of the trademark “Creative Commons” or any other trademark or logo of Creative Commons without its prior written consent including, without limitation, in connection with any unauthorized modifications to any of its public licenses or any other arrangements, understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses. 172 | > 173 | > Creative Commons may be contacted at creativecommons.org 174 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [✔]: assets/images/checkbox-small-blue.png 2 | 3 | # Node.js Best Practices 4 | 5 |

6 | Node.js Best Practices 7 |

8 | 9 |
10 | 11 |
12 | 53 items Last update: Nov 15, 2017 Updated for Node v.8.9 13 |
14 | 15 |
16 | 17 | [![nodepractices](/assets/images/twitter-s.png)](https://twitter.com/nodepractices/) **Follow us on Twitter!** [**@nodepractices**](https://twitter.com/nodepractices/) 18 |
19 | 20 | # Welcome! 3 Things You Ought To Know First: 21 | **1. When you read here, you in fact read dozens of the best Node.JS articles -** this is a summary and curation of the top-ranked content on Node JS best practices 22 | 23 | **2. It is the largest compilation, and it is growing every week -** currently, more than 50 best practices, style guides, and architectural tips are presented. New issues and PR are created every day to keep this live book updated. We'd love to see you contributing here, whether fixing some code mistake or suggesting brilliant new ideas. See our [milestones here](https://github.com/i0natan/nodebestpractices/milestones?direction=asc&sort=due_date&state=open) 24 | 25 | **3. Most bullets have additional info -** nearby most best practice bullets you'll find **🔗Read More** link that will present you with code examples, quotes from selected blogs and more info 26 | 27 |


28 | 29 | ## Table of Contents 30 | 1. [Project structure Practices (5)](#1-project-structure-practices) 31 | 2. [Error Handling Practices (11) ](#2-error-handling-practices) 32 | 3. [Code Style Practices (12) ](#3-code-style-practices) 33 | 4. [Testing And Overall Quality Practices (8) ](#4-testing-and-overall-quality-practices) 34 | 5. [Going To Production Practices (16) ](#5-going-to-production-practices) 35 | 6. Security Practices ([coming soon](https://github.com/i0natan/nodebestpractices/milestones?direction=asc&sort=due_date&state=open)) 36 | 7. Performance Practices ([coming soon](https://github.com/i0natan/nodebestpractices/milestones?direction=asc&sort=due_date&state=open)) 37 | 38 | 39 |


40 | # `1. Project Structure Practices` 41 | 42 | ## ![✔] 1.1 Structure your solution by components 43 | 44 | **TL;DR:** The worst large applications pitfall is maintaining a huge code base with hundreds of dependencies - such a monolith slows down developers as they try to incorporate new features. Instead, partition your code into components, each gets its own folder or a dedicated codebase, and ensure that each unit is kept small and simple. Visit 'Read More' below to see examples of correct project structure 45 | 46 | **Otherwise:** When developers who code new features struggle to realize the impact of their change and fear to break other dependant components - deployments become slower and more risky. It's also considered harder to scale-out when all the business units are not separated 47 | 48 | 🔗 [**Read More: structure by components**](/sections/projectstructre/breakintcomponents.md) 49 | 50 |

51 | 52 | ## ![✔] 1.2 Layer your components, keep Express within its boundaries 53 | 54 | **TL;DR:** Each component should contain 'layers' - a dedicated object for the web, logic and data access code. This not only draws a clean separation of concerns but also significantly eases mocking and testing the system. Though this is a very common pattern, API developers tend to mix layers by passing the web layer objects (Express req, res) to business logic and data layers - this makes your application dependant on and accessible by Express only 55 | 56 | **Otherwise:** App that mixes web objects with other layers can not be accessed by testing code, CRON jobs and other non-Express callers 57 | 58 | 🔗 [**Read More: layer your app**](/sections/projectstructre/createlayers.md) 59 | 60 |

61 | 62 | ## ![✔] 1.3 Wrap common utilities as NPM packages 63 | 64 | **TL;DR:** In a large app that constitutes a large code base, cross-cutting-concern utilities like logger, encryption and alike, should be wrapped by your own code and exposed as private NPM packages. This allows sharing them among multiple code bases and projects 65 | 66 | **Otherwise:** You'll have to invent your own deployment and dependency wheel 67 | 68 | 🔗 [**Read More: Structure by feature**](/sections/projectstructre/wraputilities.md) 69 | 70 |

71 | 72 | ## ![✔] 1.4 Separate Express 'app' and 'server' 73 | 74 | **TL;DR:** Avoid the nasty habit of defining the entire [Express](https://expressjs.com/) app in a single huge file - separate your 'Express' definition to at least two files: the API declaration (app.js) and the networking concerns (WWW). For even better structure, locate your API declaration within components 75 | 76 | **Otherwise:** Your API will be accessible for testing via HTTP calls only (slower and much harder to generate coverage reports). It probably won't be a big pleasure to maintain hundreds of lines of code in a single file 77 | 78 | 🔗 [**Read More: separate Express 'app' and 'server'**](/sections/projectstructre/separateexpress.md) 79 | 80 |

81 | 82 | ## ![✔] 1.5 Use environment aware, secure and hierarchical config 83 | 84 | 85 | **TL;DR:** A perfect and flawless configuration setup should ensure (a) keys can be read from file AND from environment variable (b) secrets are kept outside committed code (c) config is hierarchical for easier findability. There are a few packages that can help tick most of those boxes like [rc](https://www.npmjs.com/package/rc), [nconf](https://www.npmjs.com/package/nconf) and [config](https://www.npmjs.com/package/config). 86 | 87 | **Otherwise:** Failing to satisfy any of the config requirements will simply bog down the development or devops team. Probably both 88 | 89 | 🔗 [**Read More: configuration best practices**](/sections/projectstructre/configguide.md) 90 | 91 | 92 |


93 | 94 |

⬆ Return to top

95 | 96 | # `2. Error Handling Practices` 97 | 98 | ## ![✔] 2.1 Use Async-Await or promises for async error handling 99 | 100 | **TL;DR:** Handling async errors in callback style is probably the fastest way to hell (a.k.a the pyramid of doom). The best gift you can give to your code is using a reputable promise library or async-await instead which enables a much more compact and familiar code syntax like try-catch 101 | 102 | **Otherwise:** Node.JS callback style, function(err, response), is a promising way to un-maintainable code due to the mix of error handling with casual code, excessive nesting and awkward coding patterns 103 | 104 | 🔗 [**Read More: avoiding callbacks**](/sections/errorhandling/asyncerrorhandling.md) 105 | 106 |

107 | 108 | ## ![✔] 2.2 Use only the built-in Error object 109 | 110 | **TL;DR:** Many throws errors as a string or as some custom type – this complicates the error handling logic and the interoperability between modules. Whether you reject a promise, throw exception or emit error – using only the built-in Error object will increase uniformity and prevent loss of information 111 | 112 | 113 | **Otherwise:** When invoking some component, being uncertain which type of errors come in return – it makes proper error handling much harder. Even worse, using custom types to describe errors might lead to loss of critical error information like the stack trace! 114 | 115 | 🔗 [**Read More: using the built-in error object**](/sections/errorhandling/useonlythebuiltinerror.md) 116 | 117 |

118 | 119 | ## ![✔] 2.3 Distinguish operational vs programmer errors 120 | 121 | **TL;DR:** Operational errors (e.g. API received an invalid input) refer to known cases where the error impact is fully understood and can be handled thoughtfully. On the other hand, programmer error (e.g. trying to read undefined variable) refers to unknown code failures that dictate to gracefully restart the application 122 | 123 | **Otherwise:** You may always restart the application when an error appears, but why let ~5000 online users down because of a minor, predicted, operational error? the opposite is also not ideal – keeping the application up when an unknown issue (programmer error) occurred might lead to an unpredicted behavior. Differentiating the two allows acting tactfully and applying a balanced approach based on the given context 124 | 125 | 🔗 [**Read More: operational vs programmer error**](/sections/errorhandling/operationalvsprogrammererror.md) 126 | 127 |

128 | 129 | ## ![✔] 2.4 Handle errors centrally, not within an Express middleware 130 | 131 | **TL;DR:** Error handling logic such as mail to admin and logging should be encapsulated in a dedicated and centralized object that all endpoints (e.g. Express middleware, cron jobs, unit-testing) call when an error comes in. 132 | 133 | **Otherwise:** Not handling errors within a single place will lead to code duplication and probably to improperly handled errors 134 | 135 | 🔗 [**Read More: handling errors in a centralized place**](/sections/errorhandling/centralizedhandling.md) 136 | 137 |

138 | 139 | ## ![✔] 2.5 Document API errors using Swagger 140 | 141 | **TL;DR:** Let your API callers know which errors might come in return so they can handle these thoughtfully without crashing. This is usually done with REST API documentation frameworks like Swagger 142 | 143 | **Otherwise:** An API client might decide to crash and restart only because he received back an error he couldn’t understand. Note: the caller of your API might be you (very typical in a microservice environment) 144 | 145 | 146 | 🔗 [**Read More: documenting errors in Swagger**](/sections/errorhandling/documentingusingswagger.md) 147 | 148 |

149 | 150 | ## ![✔] 2.6 Shut the process gracefully when a stranger comes to town 151 | 152 | **TL;DR:** When an unknown error occurs (a developer error, see best practice number #3)- there is uncertainty about the application healthiness. A common practice suggests restarting the process carefully using a ‘restarter’ tool like Forever and PM2 153 | 154 | **Otherwise:** When an unfamiliar exception is caught, some object might be in a faulty state (e.g an event emitter which is used globally and not firing events anymore due to some internal failure) and all future requests might fail or behave crazily 155 | 156 | 🔗 [**Read More: shutting the process**](/sections/errorhandling/shuttingtheprocess.md) 157 | 158 |

159 | 160 | 161 | 162 | ## ![✔] 2.7 Use a mature logger to increase error visibility 163 | 164 | **TL;DR:** A set of mature logging tools like Winston, Bunyan or Log4J, will speed-up error discovery and understanding. So forget about console.log. 165 | 166 | **Otherwise:** Skimming through console.logs or manually through messy text file without querying tools or a decent log viewer might keep you busy at work until late 167 | 168 | 🔗 [**Read More: using a mature logger**](/sections/errorhandling/usematurelogger.md) 169 | 170 | 171 |

172 | 173 | 174 | ## ![✔] 2.8 Test error flows using your favorite test framework 175 | 176 | **TL;DR:** Whether professional automated QA or plain manual developer testing – Ensure that your code not only satisfies positive scenario but also handle and return the right errors. Testing frameworks like Mocha & Chai can handle this easily (see code examples within the "Gist popup") 177 | 178 | **Otherwise:** Without testing, whether automatically or manually, you can’t rely on our code to return the right errors. Without meaningful errors – there’s no error handling 179 | 180 | 181 | 🔗 [**Read More: testing error flows**](/sections/errorhandling/testingerrorflows.md) 182 | 183 |

184 | 185 | ## ![✔] 2.9 Discover errors and downtime using APM products 186 | 187 | **TL;DR:** Monitoring and performance products (a.k.a APM) proactively gauge your codebase or API so they can auto-magically highlight errors, crashes and slow parts that you were missing 188 | 189 | **Otherwise:** You might spend great effort on measuring API performance and downtimes, probably you’ll never be aware which are your slowest code parts under real world scenario and how these affects the UX 190 | 191 | 192 | 🔗 [**Read More: using APM products**](/sections/errorhandling/apmproducts.md) 193 | 194 |

195 | 196 | 197 | ## ![✔] 2.10 Catch unhandled promise rejections 198 | 199 | **TL;DR:** Any exception thrown within a promise will get swallowed and discarded unless a developer didn’t forget to explicitly handle. Even if your code is subscribed to process.uncaughtException! Overcome this by registering to the event process.unhandledRejection 200 | 201 | **Otherwise:** Your errors will get swallowed and leave no trace. Nothing to worry about 202 | 203 | 204 | 🔗 [**Read More: catching unhandled promise rejection**](/sections/errorhandling/catchunhandledpromiserejection.md) 205 | 206 |

207 | 208 | ## ![✔] 2.11 Fail fast, validate arguments using a dedicated library 209 | 210 | **TL;DR:** This should be part of your Express best practices – Assert API input to avoid nasty bugs that are much harder to track later. Validation code is usually tedious unless using a very cool helper libraries like Joi 211 | 212 | **Otherwise:** Consider this – your function expects a numeric argument “Discount” which the caller forgets to pass, later on your code checks if Discount!=0 (amount of allowed discount is greater than zero), then it will allow the user to enjoy a discount. OMG, what a nasty bug. Can you see it? 213 | 214 | 🔗 [**Read More: failing fast**](/sections/errorhandling/failfast.md) 215 | 216 |


217 | 218 |

⬆ Return to top

219 | 220 | # `3. Code Style Practices` 221 | 222 | ## ![✔] 3.1 Use ESLint 223 | 224 | **TL;DR:** [ESLint](https://eslint.org) is the de-facto standard for checking possible code errors and fixing code style, not only to identify nitty-gritty spacing issues but also to detect serious code anti-patterns like developers throwing errors without classification. Though ESLint can automatically fix code styles, other tools like [prettier](https://www.npmjs.com/package/prettier) and [beautify](https://www.npmjs.com/package/js-beautify) are more powerful in formatting the fix and work in conjunction with ESLint 225 | 226 | **Otherwise:** Developers will focus on tedious spacing and line-width concerns and time might be wasted overthinking about the project's code style. 227 | 228 |

229 | 230 | ## ![✔] 3.2 Node JS Specific Plugins 231 | 232 | **TL;DR:** On top of ESLint standard rules that cover vanilla JS only, add Node-specific plugins like [eslint-plugin-node](https://www.npmjs.com/package/eslint-plugin-node), [eslint-plugin-mocha](https://www.npmjs.com/package/eslint-plugin-mocha) and [eslint-plugin-node-security](https://www.npmjs.com/package/eslint-plugin-security) 233 | 234 | **Otherwise:** Many faulty Node.JS code patterns might escape under the radar. For example, developers might require(variableAsPath) files with a variable given as path which allows attackers to execute any JS script. Node.JS linters can detect such patterns and complain early 235 | 236 |

237 | 238 | ## ![✔] 3.3 Start a Codeblock's Curly Braces in the Same Line 239 | 240 | **TL;DR:** The opening curly braces of a code block should be in the same line of the opening statement. 241 | 242 | ### Code Example 243 | ```javascript 244 | // Do 245 | function someFunction() { 246 | // code block 247 | } 248 | 249 | // Avoid 250 | function someFunction() 251 | { 252 | // code block 253 | } 254 | ``` 255 | 256 | **Otherwise:** Deferring from this best practice might lead to unexpected results, as seen in the Stackoverflow thread below: 257 | 258 | 🔗 [**Read more:** "Why does a results vary based on curly brace placement?" (Stackoverflow)](https://stackoverflow.com/questions/3641519/why-does-a-results-vary-based-on-curly-brace-placement) 259 | 260 |

261 | 262 | ## ![✔] 3.4 Don't Forget the Semicolon 263 | 264 | **TL;DR:** While not unanimously agreed upon, it is still recommended to put a semicolon at the end of each statement. This will make your code more readable and explicit to other developers who read it. 265 | 266 | **Otherwise:** As seen in the previous section, JavaScript's interpreter automatically adds a semicolon at the end of a statement if there isn't one which might lead to some undesired results. 267 | 268 |

269 | 270 | ## ![✔] 3.5 Name Your Functions 271 | 272 | **TL;DR:** Name all functions, including closures and callbacks. Avoid anonymous functions. This is especially useful when profiling a node app. Naming all functions will allow you to easily understand what you're looking at when checking a memory snapshot. 273 | 274 | **Otherwise:** Debugging production issues using a core dump (memory snapshot) might become challenging as you notice significant memory consumption from anonymous functions. 275 | 276 |

277 | 278 | ## ![✔] 3.6 Naming conventions for variables, constants, functions and classes 279 | 280 | **TL;DR:** Use ***lowerCamelCase*** when naming constants, variables and functions and ***UpperCamelCase*** (capital first letter as well) when naming classes. This will help you to easily distinguish between plain variables / functions, and classes that require instantiation. Use descriptive names, but try to keep them short. 281 | 282 | **Otherwise:** Javascript is the only language in the world which allows to invoke a constructor ("Class") directly without instantiating it first. Consequently, Classes and function-constructors are differentiated by starting with UpperCamelCase. 283 | 284 | ### Code Example ### 285 | ```javascript 286 | // for class name we use UpperCamelCase 287 | class SomeClassExample {} 288 | 289 | // for const names we use the const keyword and lowerCamelCase 290 | const config = { 291 | key: 'value' 292 | }; 293 | 294 | // for variables and functions names we use lowerCamelCase 295 | let someVariableExample = 'value'; 296 | function doSomething() {} 297 | ``` 298 | 299 |

300 | 301 | ## ![✔] 3.7 Prefer const over let. Ditch the var 302 | 303 | **TL;DR:** Using `const` means that once a variable is assigned, it cannot be reassigned. Preferring const will help you to not be tempted to use the same variable for different uses, and make your code clearer. If a variable needs to be reassigned, in a for loop for example, use `let` to declare it. Another important aspect of let is that a variable declared using let is only available in the block scope in which it was defined. `var` is function scoped, not block scoped, and [shouldn't be used in ES6](https://hackernoon.com/why-you-shouldnt-use-var-anymore-f109a58b9b70) now that you have const and let at your disposal. 304 | 305 | **Otherwise:** Debugging becomes way more cumbersome when following a variable that frequently changes. 306 | 307 | 🔗 [**Read more: JavaScript ES6+: var, let, or const?** ](https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75) 308 | 309 |

310 | 311 | ## ![✔] 3.8 Requires come first, and not inside functions 312 | 313 | **TL;DR:** Require modules at the beginning of each file, before and outside of any functions. This simple best practice will not only help you easily and quickly tell the dependencies of a file right at the top, but also avoids a couple of potential problems. 314 | 315 | **Otherwise:** Requires are run synchronously by NodeJS. If they are called from within a function, it may block other requests from being handled at a more critical time. Also, if a required module or any of its own dependencies throw an error and crash the server, it is best to find out about it as soon as possible, which might not be the case if that module is required from within a function. 316 | 317 |

318 | 319 | ## ![✔] 3.9 Do Require on the folders, not directly on the files 320 | 321 | **TL;DR:** When developing a module/library in a folder, place an index.js file that exposes the module's 322 | internals so every consumer will pass through it. This serves as an 'interface' to your module and ease 323 | future changes without breaking the contract. 324 | 325 | **Otherwise:** Changing to the internal structure of files or the signature may break the interface with 326 | clients. 327 | 328 | ### Code example 329 | ```javascript 330 | // Do 331 | module.exports.SMSProvider = require('./SMSProvider'); 332 | module.exports.SMSNumberResolver = require('./SMSNumberResolver'); 333 | 334 | // Avoid 335 | module.exports.SMSProvider = require('./SMSProvider/SMSProvider.js'); 336 | module.exports.SMSNumberResolver = require('./SMSNumberResolver/SMSNumberResolver.js'); 337 | ``` 338 | 339 |

340 | 341 | 342 | ## ![✔] 3.10 Use the `===` operator 343 | 344 | **TL;DR:** Prefer the strict equality operator `===` over the weaker abstract equality operator `==`. `==` will compare two variables after converting them to a common type. There is no type conversion in `===`, and both variables must be of the same type to be equal. 345 | 346 | **Otherwise:** Unequal variables might return true when compared with the `==` operator. 347 | 348 | ### Code example 349 | ```javascript 350 | '' == '0' // false 351 | 0 == '' // true 352 | 0 == '0' // true 353 | 354 | false == 'false' // false 355 | false == '0' // true 356 | 357 | false == undefined // false 358 | false == null // false 359 | null == undefined // true 360 | 361 | ' \t\r\n ' == 0 // true 362 | ``` 363 | All statements above will return false if used with `===` 364 | 365 |

366 | 367 | ## ![✔] 3.11 Use Async Await, avoid callbacks 368 | 369 | **TL;DR:** Node 8 LTS now has full support for Async-await. This is a new way of dealing with asynchronous code which supersedes callbacks and promises. Async-await is non-blocking, and it makes asynchronous code look synchronous. The best gift you can give to your code is using async-await which provides a much more compact and familiar code syntax like try-catch. 370 | 371 | **Otherwise:** Handling async errors in callback style is probably the fastest way to hell - this style forces to check errors all over, deal with awkward code nesting and make it difficult to reason about the code flow. 372 | 373 | 🔗[**Read more:** Guide to async await 1.0](https://github.com/yortus/asyncawait) 374 | 375 |

376 | 377 | ## ![✔] 3.12 Use Fat (=>) Arrow Functions 378 | 379 | **TL;DR:** Though it's recommended to use async-await and avoid function parameters, when dealing with older API that accept promises or callbacks - arrow functions make the code structure more compact and keep the lexical context of the root function (i.e. 'this'). 380 | 381 | **Otherwise:** Longer code (in ES5 functions) is more prone to bugs and cumbersome to read. 382 | 383 | 🔗 [**Read mode: It’s Time to Embrace Arrow Functions**](https://medium.com/javascript-scene/familiarity-bias-is-holding-you-back-its-time-to-embrace-arrow-functions-3d37e1a9bb75) 384 | 385 | 386 |


387 | 388 |

⬆ Return to top

389 | 390 | 391 | # `4. Testing And Overall Quality Practices` 392 | 393 | ## ![✔] 4.1 At the very least, write API (component) testing 394 | 395 | **TL;DR:** Most projects just don't have any automated testing due to short time tables or often the 'testing project' run out of control and being abandoned. For that reason, prioritize and start with API testing which are the easiest to write and provide more coverage than unit testing (you may even craft API tests without code using tools like [Postman](https://www.getpostman.com/). Afterwards, should you have more resources and time, continue with advanced test types like unit testing, DB testing, performance testing, etc 396 | 397 | **Otherwise:** You may spend long days on writing unit tests to find out that you got only 20% system coverage 398 | 399 |

400 | 401 | ## ![✔] 4.2 Detect code issues with a linter 402 | 403 | **TL;DR:** Use a code linter to check basic quality and detect anti-patterns early. Run it before any test and add it as a pre-commit git-hook to minimize the time needed to review and correct any issue. Also check [Section 3](https://github.com/i0natan/nodebestpractices#3-code-style-practices) on Code Style Practices 404 | 405 | **Otherwise:** You may let pass some anti-pattern and possible vulnerable code to your production environment. 406 | 407 | 408 |

409 | 410 | ## ![✔] 4.3 Carefully choose your CI platform (Jenkins vs CircleCI vs Travis vs Rest of the world) 411 | 412 | **TL;DR:** Your continuous integration platform (CICD) will host all the quality tools (e.g test, lint) so it should come with a vibrant ecosystem of plugins. [Jenkins](https://jenkins.io/) used to be the default for many projects as it has the biggest community along with a very powerful platform at the price of complex setup that demands a steep learning curve. Nowadays, it became much easier to setup a CI solution using SaaS tools like [CircleCI](https://circleci.com) and others. These tools allow crafting a flexible CI pipeline without the burden of managing the whole infrastructure. Eventually, it's a trade-off between robustness and speed - choose your side carefully. 413 | 414 | **Otherwise:** Choosing some niche vendor might get you blocked once you need some advanced customization. On the other hand, going with Jenkins might burn precious time on infrastructure setup 415 | 416 | 🔗 [**Read More: Choosing CI platform**](/sections/testingandquality/citools.md) 417 | 418 |

419 | 420 | ## ![✔] 4.4 Constantly inspect for vulnerable dependencies 421 | 422 | **TL;DR:** Even the most reputable dependencies such as Express have known vulnerabilities. This can get easily tamed using community and commercial tools such as 🔗 [nsp](https://github.com/nodesecurity/nsp) that can be invoked from your CI on every build 423 | 424 | **Otherwise:** Keeping your code clean from vulnerabilities without dedicated tools will require to constantly follow online publications about new threats. Quite tedious 425 | 426 |

427 | 428 | ## ![✔] 4.5 Tag your tests 429 | 430 | **TL;DR:** Different tests must run on different scenarios: quick smoke, IO-less, tests should run when a developer saves or commits a file, full end-to-end tests usually run when a new pull request is submitted, etc. This can be achieved by tagging tests with keywords like #cold #api #sanity so you can grep with your testing harness and invoke the desired subset. For example, this is how you would invoke only the sanity test group with [Mocha](https://mochajs.org/): mocha --grep 'sanity' 431 | 432 | **Otherwise:** Running all the tests, including tests that perform dozens of DB queries, any time a developer makes a small change can be extremely slow and keeps developers away from running tests 433 | 434 |

435 | 436 | ## ![✔] 4.6 Check your test coverage, it helps to identify wrong test patterns 437 | 438 | **TL;DR:** Code coverage tools like [Istanbul/NYC ](https://github.com/gotwarlost/istanbul)are great for 3 reasons: it comes for free (no effort is required to benefit this reports), it helps to identify a decrease in testing coverage, and last but not least it highlights testing mismatches: by looking at colored code coverage reports you may notice, for example, code areas that are never tested like catch clauses (meaning that tests only invoke the happy paths and not how the app behaves on errors). Set it to fail builds if the coverage falls under a certain threshold 439 | 440 | **Otherwise:** There won't be any automated metric telling you when a large portion of your code is not covered by testing 441 | 442 | 443 | 444 |

445 | 446 | ## ![✔] 4.7 Inspect for outdated packages 447 | 448 | **TL;DR:** Use your preferred tool (e.g. 'npm outdated' or [npm-check-updates](https://www.npmjs.com/package/npm-check-updates) to detect installed packages which are outdated, inject this check into your CI pipeline and even make a build fail in a severe scenario. For example, a severe scenario might be when an installed package is 5 patch commits behind (e.g. local version is 1.3.1 and repository version is 1.3.8) or it is tagged as deprecated by its author - kill the build and prevent deploying this version 449 | 450 | **Otherwise:** Your production will run packages that have been explicitly tagged by their author as risky 451 | 452 |

453 | 454 | ## ![✔] 4.8 Use docker-compose for e2e testing 455 | 456 | **TL;DR:** End to end (e2e) testing which includes live data used to be the weakest link of the CI process as it depends on multiple heavy services like DB. Docker-compose turns this problem into a breeze by crafting production-like environment using a simple text file and easy commands. It allows crafting all the dependent services, DB and isolated network for e2e testing. Last but not least, it can keep a stateless environment that is invoked before each test suite and dies right after 457 | 458 | 459 | **Otherwise:** Without docker-compose teams must maintain a testing DB for each testing environment including developers machines, keep all those DBs in sync so test results won't vary across environments 460 | 461 | 462 |


463 | 464 |

⬆ Return to top

465 | 466 | # `5. Going To Production Practices` 467 | ## ![✔] 5.1. Monitoring! 468 | 469 | **TL;DR:** Monitoring is a game of finding out issues before customers do – obviously this should be assigned unprecedented importance. The market is overwhelmed with offers thus consider starting with defining the basic metrics you must follow (my suggestions inside), then go over additional fancy features and choose the solution that ticks all boxes. Click ‘The Gist’ below for overview of solutions 470 | 471 | **Otherwise:** Failure === disappointed customers. Simple. 472 | 473 | 474 | 🔗 [**Read More: Monitoring!**](/sections/production/monitoring.md) 475 | 476 |

477 | 478 | ## ![✔] 5.2. Increase transparency using smart logging 479 | 480 | **TL;DR:** Logs can be a dumb warehouse of debug statements or the enabler of a beautiful dashboard that tells the story of your app. Plan your logging platform from day 1: how logs are collected, stored and analyzed to ensure that the desired information (e.g. error rate, following an entire transaction through services and servers, etc) can really be extracted 481 | 482 | **Otherwise:** You end-up with a blackbox that is hard to reason about, then you start re-writing all logging statements to add additional information 483 | 484 | 485 | 🔗 [**Read More: Increase transparency using smart logging**](/sections/production/smartlogging.md) 486 | 487 |

488 | 489 | ## ![✔] 5.3. Delegate anything possible (e.g. gzip, SSL) to a reverse proxy 490 | 491 | **TL;DR:** Node is awfully bad at doing CPU intensive tasks like gzipping, SSL termination, etc. You should use ‘real’ middleware services like nginx, HAproxy or cloud vendor services instead 492 | 493 | **Otherwise:** Your poor single thread will stay busy doing infrastructural tasks instead of dealing with your application core and performance will degrade accordingly 494 | 495 | 496 | 🔗 [**Read More: Delegate anything possible (e.g. gzip, SSL) to a reverse proxy**](/sections/production/delegatetoproxy.md) 497 | 498 |

499 | 500 | ## ![✔] 5.4. Lock dependencies 501 | 502 | **TL;DR:** Your code must be identical across all environments, but amazingly NPM lets dependencies drift across environments by default – when you install packages at various environments it tries to fetch packages’ latest patch version. Overcome this by using NPM config files , .npmrc, that tell each environment to save the exact (not the latest) version of each package. Alternatively, for finer grain control use NPM” shrinkwrap”. *Update: as of NPM5 , dependencies are locked by default. The new package manager in town, Yarn, also got us covered by default 503 | 504 | **Otherwise:** QA will thoroughly test the code and approve a version that will behave differently at production. Even worse, different servers at the same production cluster might run different code 505 | 506 | 507 | 🔗 [**Read More: Lock dependencies**](/sections/production/lockdependencies.md) 508 | 509 |

510 | 511 | ## ![✔] 5.5. Guard process uptime using the right tool 512 | 513 | **TL;DR:** The process must go on and get restarted upon failures. For simple scenario, ‘restarter’ tools like PM2 might be enough but in today ‘dockerized’ world – a cluster management tools should be considered as well 514 | 515 | **Otherwise:** Running dozens of instances without clear strategy and too many tools together (cluster management, docker, PM2) might lead to a devops chaos 516 | 517 | 518 | 🔗 [**Read More: Guard process uptime using the right tool**](/sections/production/guardprocess.md) 519 | 520 | 521 |

522 | 523 | ## ![✔] 5.6. Utilize all CPU cores 524 | 525 | **TL;DR:** At its basic form, a Node app runs on a single CPU core while all other are left idling. It’s your duty to replicate the Node process and utilize all CPUs – For small-medium apps you may use Node Cluster or PM2. For a larger app consider replicating the process using some Docker cluster (e.g. K8S, ECS) or deployment scripts that are based on Linux init system (e.g. systemd) 526 | 527 | **Otherwise:** Your app will likely utilize only 25% of its available resources(!) or even less. Note that a typical server has 4 CPU cores or more, naive deployment of Node.JS utilizes only 1 (even using PaaS services like AWS beanstalk!) 528 | 529 | 530 | 🔗 [**Read More: Utilize all CPU cores**](/sections/production/utilizecpu.md) 531 | 532 |

533 | 534 | ## ![✔] 5.7. Create a ‘maintenance endpoint’ 535 | 536 | **TL;DR:** Expose a set of system-related information, like memory usage and REPL, etc in a secured API. Although it’s highly recommended to rely on standard and battle-tests tools, some valuable information and operations are easier done using code 537 | 538 | **Otherwise:** You’ll find that you’re performing many “diagnostic deploys” – shipping code to production only to extract some information for diagnostic purposes 539 | 540 | 541 | 🔗 [**Read More: Create a ‘maintenance endpoint’**](/sections/production/createmaintenanceendpoint.md) 542 | 543 |

544 | 545 | ## ![✔] 5.8. Discover errors and downtime using APM products 546 | 547 | **TL;DR:** Monitoring and performance products (a.k.a APM) proactively gauge codebase and API so they can auto-magically go beyond traditional monitoring and measure the overall user-experience across services and tiers. For example, some APM products can highlight a transaction that loads too slow on the end-users side while suggesting the root cause 548 | 549 | **Otherwise:** You might spend great effort on measuring API performance and downtimes, probably you’ll never be aware which is your slowest code parts under real world scenario and how these affects the UX 550 | 551 | 552 | 🔗 [**Read More: Discover errors and downtime using APM products**](/sections/production/apmproducts.md) 553 | 554 | 555 |

556 | 557 | 558 | ## ![✔] 5.9. Make your code production-ready 559 | 560 | **TL;DR:** Code with the end in mind, plan for production from day 1. This sounds a bit vague so I’ve compiled a few development tips that are closely related to production maintenance (click Gist below) 561 | 562 | **Otherwise:** A world champion IT/devops guy won’t save a system that is badly written 563 | 564 | 565 | 🔗 [**Read More: Make your code production-ready**](/sections/production/productoncode.md) 566 | 567 |

568 | 569 | ## ![✔] 5.10. Measure and guard the memory usage 570 | 571 | **TL;DR:** Node.js has controversial relationships with memory: the v8 engine has soft limits on memory usage (1.4GB) and there are known paths to leaks memory in Node’s code – thus watching Node’s process memory is a must. In small apps you may gauge memory periodically using shell commands but in medium-large app consider baking your memory watch into a robust monitoring system 572 | 573 | **Otherwise:** Your process memory might leak a hundred megabytes a day like happened in Wallmart 574 | 575 | 576 | 🔗 [**Read More: Measure and guard the memory usage**](/sections/production/measurememory.md) 577 | 578 |

579 | 580 | 581 | ## ![✔] 5.11. Get your frontend assets out of Node 582 | 583 | **TL;DR:** Serve frontend content using dedicated middleware (nginx, S3, CDN) because Node performance really gets hurt when dealing with many static files due to its single threaded model 584 | 585 | **Otherwise:** Your single Node thread will be busy streaming hundreds of html/images/angular/react files instead of allocating all its resources for the task it was born for – serving dynamic content 586 | 587 | 588 | 🔗 [**Read More: Get your frontend assets out of Node**](/sections/production/frontendout.md) 589 | 590 |

591 | 592 | 593 | ## ![✔] 5.12. Be stateless, kill your Servers almost every day 594 | 595 | **TL;DR:** Store any type of data (e.g. users session, cache, uploaded files) within external data stores. Consider ‘killing’ your servers periodically or use ‘serverless’ platform (e.g. AWS Lambda) that explicitly enforces a stateless behavior 596 | 597 | **Otherwise:** Failure at a given server will result in application downtime instead of just killing a faulty machine. Moreover, scaling-out elasticity will get more challenging due to the reliance on a specific server 598 | 599 | 600 | 🔗 [**Read More: Be stateless, kill your Servers almost every day**](/sections/production/bestateless.md) 601 | 602 | 603 |

604 | 605 | 606 | ## ![✔] 5.13. Use tools that automatically detect vulnerabilities 607 | 608 | **TL;DR:** Even the most reputable dependencies such as Express have known vulnerabilities (from time to time) that can put a system at risk. This can get easily tamed using community and commercial tools that constantly check for vulnerabilities and warn (locally or at GitHub), some can even patch them immediately 609 | 610 | **Otherwise:** Otherwise: Keeping your code clean from vulnerabilities without dedicated tools will require to constantly follow online publications about new threats. Quite tedious 611 | 612 | 613 | 🔗 [**Read More: Use tools that automatically detect vulnerabilities**](/sections/production/detectvulnerabilities.md) 614 | 615 |

616 | 617 | 618 | ## ![✔] 5.14. Assign ‘TransactionId’ to each log statement 619 | 620 | **TL;DR:** Assign the same identifier, transaction-id: {some value}, to each log entry within a single request. Then when inspecting errors in logs, easily conclude what happened before and after. Unfortunately, this is not easy to achieve in Node due its async nature, see code examples inside 621 | 622 | **Otherwise:** Looking at a production error log without the context – what happened before – makes it much harder and slower to reason about the issue 623 | 624 | 625 | 🔗 [**Read More: Assign ‘TransactionId’ to each log statement**](/sections/production/assigntransactionid.md) 626 | 627 |

628 | 629 | 630 | ## ![✔] 5.15. Set NODE_ENV=production 631 | 632 | **TL;DR:** Set the environment variable NODE_ENV to ‘production’ or ‘development’ to flag whether production optimizations should get activated – many NPM packages determining the current environment and optimize their code for production 633 | 634 | **Otherwise:** Omitting this simple property might greatly degrade performance. For example, when using Express for server side rendering omitting NODE_ENV makes the slower by a factor of three! 635 | 636 | 637 | 🔗 [**Read More: Set NODE_ENV=production**](/sections/production/setnodeenv.md) 638 | 639 | 640 |

641 | 642 | 643 | ## ![✔] 5.16. Design automated, atomic and zero-downtime deployments 644 | 645 | **TL;DR:** Researches show that teams who perform many deployments – lowers the probability of severe production issues. Fast and automated deployments that don’t require risky manual steps and service downtime significantly improves the deployment process. You should probably achieve that using Docker combined with CI tools as they became the industry standard for streamlined deployment 646 | 647 | **Otherwise:** Long deployments -> production down time & human-related error -> team unconfident and in making deployment -> less deployments and features 648 | 649 |


650 | 651 |

⬆ Return to top

652 | 653 | # `Security Practices` 654 | 655 | ## Our contributors are working on this section. Would you like to join? 656 | 657 |


658 | # `Performance Practices` 659 | 660 | ## Our contributors are working on this section. Would you like to join? 661 | 662 | 663 |

664 | 665 | # Milestones 666 | To maintain this guide and keep it up to date, we are constantly updating and improving the guidelines and best practices with the help of the community. You can follow our [milestones](https://github.com/i0natan/nodebestpractices/milestones) and join the working groups if you want to contribute to this project. 667 | 668 |

669 | 670 | # Contributors 671 | ## `Yoni Goldberg` 672 | Independent Node.JS consultant who works with customers at USA, Europe and Israel on building large-scale scalable Node applications. Many of the best practices above were first published on his blog post at [http://www.goldbergyoni.com](http://www.goldbergyoni.com). Reach Yoni at @goldbergyoni or me@goldbergyoni.com 673 | 674 | ## `Ido Richter` 675 | 👨‍💻 Software engineer, 🌐 web developer, 🤖 emojis enthusiast. 676 | 677 | ## `Refael Ackermann` [@refack](https://github.com/refack) <refack@gmail.com> (he/him) 678 | Node.js Core Collaborator, been noding since 0.4, and have noded in multiple production sites. Founded `node4good` home of [`lodash-contrib`](https://github.com/node4good/lodash-contrib), [`formage`](https://github.com/node4good/formage), and [`asynctrace`](https://github.com/node4good/asynctrace). 679 | `refack` on freenode, Twitter, GitHub, GMail, and many other platforms. DMs are open, happy to help. 680 | 681 | ## `Bruno Scheufler` 682 | 💻 full-stack web developer and Node.js enthusiast. 683 | 684 | 685 |

686 | 687 | # Thank You Notes 688 | 689 | This repository is being kept up to date thanks to the help from the community. We appreciate any contribution, from a single word fix to a new best practice. Below is a list of everyone who contributed to this project. A :sunflower: marks a successful pull request and a :star: marks an approved new best practice. 690 | 691 | 🌻 [Kevin Rambaud](https://github.com/kevinrambaud), 692 | 🌻 [Michael Fine](https://github.com/mfine15), 693 | 🌻 [Shreya Dahal](https://github.com/squgeim), 694 | 🌻 [ChangJoo Park](https://github.com/ChangJoo-Park), 695 | 🌻 [Matheus Cruz Rocha](https://github.com/matheusrocha89), 696 | 🌻 [Yog Mehta](https://github.com/BitYog), 697 | 🌻 [Kudakwashe Paradzayi](https://github.com/kudapara), 698 | 🌻 [t1st3](https://github.com/t1st3), 699 | 🌻 [mulijordan1976](https://github.com/mulijordan1976), 700 | 🌻 [Matan Kushner](https://github.com/matchai), 701 | 🌻 [Fabio Hiroki](https://github.com/fabiothiroki), 702 | 🌻 [James Sumners](https://github.com/jsumners), 703 | 🌻 [Chandan Rai](https://github.com/crowchirp), 704 | 🌻 [Dan Gamble](https://github.com/dan-gamble), 705 | 🌻 [PJ Trainor](https://github.com/trainorpj), 706 | 🌻 [Remek Ambroziak](https://github.com/reod), 707 | 🌻 [Yoni Jah](https://github.com/yonjah), 708 | 🌻 [Misha Khokhlov](https://github.com/hazolsky), 709 | 🌻 [Evgeny Orekhov](https://github.com/EvgenyOrekhov), 710 | 🌻 [Gediminas Petrikas](https://github.com/gediminasml), 711 | 🌻 [Isaac Halvorson](https://github.com/hisaac), 712 | 🌻 [Vedran Karačić](https://github.com/vkaracic), 713 | 🌻 [lallenlowe](https://github.com/lallenlowe), 714 | 🌻 [Nathan Wells](https://github.com/nwwells), 715 | 🌻 [Paulo Vítor S Reis](https://github.com/paulovitin), 716 | 🌻 [syzer](https://github.com/syzer), 717 | 🌻 [David Sancho](https://github.com/davesnx), 718 | 🌻 [Robert Manolea](https://github.com/pupix), 719 | 🌻 [Xavier Ho](https://github.com/spaxe), 720 | 🌻 [Aaron Arney](https://github.com/ocularrhythm), 721 | 🌻 [Jan Charles Maghirang Adona](https://github.com/septa97), 722 | 🌻 [Allen Fang](https://github.com/AllenFang), 723 | 🌻 [Leonardo Villela](https://github.com/leonardovillela) 724 | 725 | 726 | 727 |

728 | ## :star: No Stars Yet, Waiting For The First To Suggest a New Bullet 729 | 730 | --------------------------------------------------------------------------------