TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Node.js Best Practices

220 pointsby finspinover 10 years ago

10 comments

Toucheover 10 years ago
Notice something not on the list of best practices: documentation. While Node itself has excellent docs (for the most part), the Node community is <i></i>terrible<i></i> about documentation. If you&#x27;re lucky you&#x27;ll get a single README.md file with the basics covered.<p>Pick any category of module and there&#x27;s a good chance the most popular modules have little documentation; certainly nothing close to comprehensive.
评论 #8342759 未加载
评论 #8344078 未加载
评论 #8342941 未加载
评论 #8343378 未加载
评论 #8347121 未加载
general_failureover 10 years ago
Just a few comments:<p>&gt; if (!(this instanceof MyClass)) return new MyClass();<p>If you really want, just throw an exception and kill the program at compile time. Catch programming errors in testing and not do some magic to &#x27;autocorrect&#x27; code.<p>&gt; var localFile = fs.createWriteStream(&#x27;localFile.tmp&#x27;);<p>Always catch &#x27;error&#x27;s in stream objects. Otherwise, it might thrown an exception at runtime.<p>localFile.on(&#x27;error&#x27;, &#x2F;* do something *&#x2F;)<p>Coding style: In most cases if you write you code properly, you don&#x27;t need to nest more than 3-4 levels. If it gets deeper split it out into separate functions. Otherwise, it&#x27;s a perfect job for async.series.
评论 #8342588 未加载
Kiroover 10 years ago
I&#x27;m coding a fairly large application in Node and have never heard of EventEmitter or Streams. Does that mean I&#x27;m doing something wrong? The impression I get from the article is that it&#x27;s such a fundamental patterns that every serious application should use it.
评论 #8342528 未加载
评论 #8342481 未加载
评论 #8343584 未加载
whatthemickover 10 years ago
I&#x27;m personally not a huge fan of the eventemitter for cases where an event is only ever emitted once (an sql query is done or similar).<p>For 2.0 of Sequelize we&#x27;ve moved almost the entire codebase to promises and will encourage users to interact with Sequelize with promises.
评论 #8344620 未加载
pavlovover 10 years ago
Not that long ago, anonymous functions and closures were the height of fashion in every language.<p>Now the recommendation is to name your functions and avoid closures. This brings us right back to what C programmers have been doing with function pointers since forever. Not that this is such a bad thing.
pedalpeteover 10 years ago
When they say &#x27;avoiding closures&#x27;, how does that relate to functions in your module? Your module is often exported as a function, so are they suggesting that every function be exported? I suspect I&#x27;m not understanding the logic behind &#x27;stack based&#x27;. Why is it better to have your functions not contain other functions (or is it not be contained?)
评论 #8343055 未加载
评论 #8343520 未加载
donbronsonover 10 years ago
Anyone have more insight into the statement &quot;Avoid closures&quot;? Or rather, an alternative to having private functions?
评论 #8342418 未加载
评论 #8343550 未加载
评论 #8342371 未加载
评论 #8342796 未加载
评论 #8342373 未加载
EGregover 10 years ago
Very useful!
_random_over 10 years ago
0. Avoid using it, unless truly necessary.
passfreeover 10 years ago
I have one word: CoffeeScript.
评论 #8343206 未加载
评论 #8343207 未加载
评论 #8342554 未加载