mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-29 08:37:18 +08:00
Saved README.md with Dillinger.io
This commit is contained in:
209
README.md
209
README.md
@ -1,175 +1,84 @@
|
||||
# `Node.JS Best Practices`
|
||||
<!--- # Node.JS Best Practices -->
|
||||
<!---  -->
|
||||
<h1 align="center">
|
||||
<img src="assets/images/banner-2.jpg" alt="Node.js Best Practices" />
|
||||
</h1>
|
||||
|
||||

|
||||
<img src="https://img.shields.io/badge/⚙%20Item%20count%20-%2053%20Best%20practices-blue.svg" alt="53 items"> <img src="https://img.shields.io/badge/%F0%9F%93%85%20Last%20update%20-%206%20days%20ago-green.svg" alt="Last update: 7 days ago"> <img src="https://img.shields.io/badge/%E2%9C%94%20Updated%20For%20Version%20-%20Node%208.4-brightgreen.svg" alt="Updated for Node v.8.4">
|
||||
|
||||
Welcome
|
||||
-------------
|
||||
<!---  -->
|
||||
|
||||
Some text here around. Ido: testing icon from StackEdit: <i class="icon-folder-open"></i>
|
||||
# Welcome to Node.js Best Practices
|
||||
|
||||
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.
|
||||
|
||||
## Table of Contents
|
||||
* [Project Setup Practices (18)](#project-setup-practices)
|
||||
* [Code Style Practices (11) ](#code-style-practices)
|
||||
* [Error Handling Practices (14) ](#error-handling-practices)
|
||||
* [Going To Production Practices (21) ](#going-to-production-practices)
|
||||
* [Testing Practices (9) ](#deployment-practices)
|
||||
* [Security Practices (8) ](#security-practices)
|
||||
|
||||
|
||||
Project Setup Practices
|
||||
=======================
|
||||
<br/><br/>
|
||||
# `Project Setup Practices`
|
||||
|
||||
:white_check_mark: 1. Do something important
|
||||
-------------------------
|
||||
## ✔ 1. Structure your solution by feature ('microservices')
|
||||
|
||||
**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.
|
||||
|
||||
**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.
|
||||
|
||||
🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md)
|
||||
|
||||
<br/><br/>
|
||||
|
||||
## ✔ 2. Layer your app, keep Express within its boundaries
|
||||
|
||||
**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
|
||||
|
||||
**Otherwise:** Application can be accessed by Express only and require to create complex testing mocks
|
||||
|
||||
🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md)
|
||||
|
||||
<br/><br/>
|
||||
|
||||
## ✔ 3. Configure ESLint with node-specific plugins
|
||||
|
||||
**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
|
||||
|
||||
**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
|
||||
|
||||
 2. Do other thing
|
||||
-------------------------
|
||||
|
||||
**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
|
||||
|
||||
**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
|
||||
🔗 [**Read More: Structure by feature*](/sections/errorhandling/asyncawait.md)
|
||||
|
||||
|
||||
Code Style Practices
|
||||
-------------
|
||||
<br/><br/><br/>
|
||||
# `Code Style Practices`
|
||||
|
||||
Error Handling Practices
|
||||
-------------
|
||||
|
||||
Going To Production Practices
|
||||
-------------
|
||||
<br/><br/><br/>
|
||||
# `Error Handling Practices`
|
||||
<p align="right"><a href="#table-of-contents">⬆ Return to top</a></p>
|
||||
|
||||
Deployment Practices
|
||||
-------------
|
||||
## ✔ Use async-await for async error handling
|
||||
|
||||
Security Practices
|
||||
-------------
|
||||
* **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
|
||||
|
||||
* **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
|
||||
|
||||
🔗 [**Use async-await for async error handling**](/sections/errorhandling/asyncawait.md)
|
||||
|
||||
|
||||
|
||||
|
||||
| Item | Value | Qty |
|
||||
| :------- | ----: | :---: |
|
||||
| Computer | $1600 | 5 |
|
||||
| Phone | $12 | 12 |
|
||||
| Pipe | $1 | 234 |
|
||||
<br/><br/><br/>
|
||||
# `Going To Production Practices`
|
||||
|
||||
|
||||
### Definition Lists
|
||||
|
||||
**Markdown Extra** has a special syntax for definition lists too:
|
||||
|
||||
Term 1
|
||||
Term 2
|
||||
: Definition A
|
||||
: Definition B
|
||||
|
||||
Term 3
|
||||
|
||||
: Definition C
|
||||
|
||||
: Definition D
|
||||
|
||||
> part of definition D
|
||||
<br/><br/><br/>
|
||||
# `Deployment Practices`
|
||||
|
||||
|
||||
### Fenced code blocks
|
||||
<br/><br/><br/>
|
||||
# `Security Practices`
|
||||
|
||||
GitHub's fenced code blocks are also supported with **Highlight.js** syntax highlighting:
|
||||
|
||||
```
|
||||
// Foo
|
||||
var bar = 0;
|
||||
```
|
||||
|
||||
> **Tip:** To use **Prettify** instead of **Highlight.js**, just configure the **Markdown Extra** extension in the <i class="icon-cog"></i> **Settings** dialog.
|
||||
|
||||
> **Note:** You can find more information:
|
||||
|
||||
> - about **Prettify** syntax highlighting [here][5],
|
||||
> - about **Highlight.js** syntax highlighting [here][6].
|
||||
|
||||
|
||||
### Footnotes
|
||||
|
||||
You can create footnotes like this[^footnote].
|
||||
|
||||
[^footnote]: Here is the *text* of the **footnote**.
|
||||
|
||||
|
||||
### SmartyPants
|
||||
|
||||
SmartyPants converts ASCII punctuation characters into "smart" typographic punctuation HTML entities. For example:
|
||||
|
||||
| | ASCII | HTML |
|
||||
----------------- | ---------------------------- | ------------------
|
||||
| Single backticks | `'Isn't this fun?'` | 'Isn't this fun?' |
|
||||
| Quotes | `"Isn't this fun?"` | "Isn't this fun?" |
|
||||
| Dashes | `-- is en-dash, --- is em-dash` | -- is en-dash, --- is em-dash |
|
||||
|
||||
|
||||
### Table of contents
|
||||
|
||||
You can insert a table of contents using the marker `[TOC]`:
|
||||
|
||||
[TOC]
|
||||
|
||||
|
||||
### MathJax
|
||||
|
||||
You can render *LaTeX* mathematical expressions using **MathJax**, as on [math.stackexchange.com][1]:
|
||||
|
||||
The *Gamma function* satisfying $\Gamma(n) = (n-1)!\quad\forall n\in\mathbb N$ is via the Euler integral
|
||||
|
||||
$$
|
||||
\Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,.
|
||||
$$
|
||||
|
||||
> **Tip:** To make sure mathematical expressions are rendered properly on your website, include **MathJax** into your template:
|
||||
|
||||
```
|
||||
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
|
||||
```
|
||||
|
||||
> **Note:** You can find more information about **LaTeX** mathematical expressions [here][4].
|
||||
|
||||
|
||||
### UML diagrams
|
||||
|
||||
You can also render sequence diagrams like this:
|
||||
|
||||
```sequence
|
||||
Alice->Bob: Hello Bob, how are you?
|
||||
Note right of Bob: Bob thinks
|
||||
Bob-->Alice: I am good thanks!
|
||||
```
|
||||
|
||||
And flow charts like this:
|
||||
|
||||
```flow
|
||||
st=>start: Start
|
||||
e=>end
|
||||
op=>operation: My Operation
|
||||
cond=>condition: Yes or No?
|
||||
|
||||
st->op->cond
|
||||
cond(yes)->e
|
||||
cond(no)->op
|
||||
```
|
||||
|
||||
> **Note:** You can find more information:
|
||||
|
||||
> - about **Sequence diagrams** syntax [here][7],
|
||||
> - about **Flow charts** syntax [here][8].
|
||||
|
||||
### Support StackEdit
|
||||
|
||||
[](https://monetizejs.com/authorize?client_id=ESTHdCYOi18iLhhO&summary=true)
|
||||
|
||||
[^stackedit]: [StackEdit](https://stackedit.io/) is a full-featured, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites.
|
||||
|
||||
|
||||
[1]: http://math.stackexchange.com/
|
||||
[2]: http://daringfireball.net/projects/markdown/syntax "Markdown"
|
||||
[3]: https://github.com/jmcmanus/pagedown-extra "Pagedown Extra"
|
||||
[4]: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
|
||||
[5]: https://code.google.com/p/google-code-prettify/
|
||||
[6]: http://highlightjs.org/
|
||||
[7]: http://bramp.github.io/js-sequence-diagrams/
|
||||
[8]: http://adrai.github.io/flowchart.js/
|
||||
|
||||
Reference in New Issue
Block a user