TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: How do you write clean node.js code?

6 点作者 michael_miller大约 14 年前
I'm working on a smallish node.js + SproutCore app now, and I'm noticing that my node.js code has gotten really ugly, really quickly as my app has grown.<p>The most common cause of ugliness so far has been DB queries. Every time I want to access a DB, I have to do something to the effect of:<p>userDB.get(uid, function(err,doc){ if(err){ // log error } // Do something with doc });<p>This is fine on a small scale, but as my app grows, it gets really hard to read the code. What if I have 5 DB queries? That is 5 levels of indentation, and a lot of extra code. I would imagine the above being written in Ruby + Rails as:<p>userData = Users.find(:uid =&#62; uid)<p>which results in no indentation, and is very easy to follow.<p>The most common solution I've heard to this is "Well, just use named functions!" This seems to produce even uglier code: because functions have to be declared <i>before</i> I can use them, I have to declare the business logic in reverse order. This means that I have to read code "backwards" to get the linear flow! And, in my experience, each named function does little more than execute a DB query.<p>It almost seems like this problem could be resolved with a little syntactic sugar:<p>userDB.get(uid) -&#62; (err, res) // Anything below here to the end of the block is part of the callback<p>Seasoned node.js developers: How do you write clean code that solves issues such as this?

3 条评论

m0hit大约 14 年前
I am not a seasoned node.js developer, but have been playing around with a small node.js + socket.io + mongodb based application over the last month or so.<p>Don't think I have direct answers to your question, but here is how I have been learning:<p>- the node.js boilerplates available on github are a useful starting point for organizing your code <a href="https://github.com/robrighter/node-boilerplate" rel="nofollow">https://github.com/robrighter/node-boilerplate</a>, <a href="https://github.com/mape/node-express-boilerplate" rel="nofollow">https://github.com/mape/node-express-boilerplate</a> There are differences in how the code is structured, but they are great to learn from.<p>- Break up your project into libraries. In many cases the very basic examples do not break out the code appropriately for simplicity. However, there are some great examples within express source (i can't remember the exact name) that organize web applications routes into multiple files (and modules).<p>- node.js has tons of well written code on github. I've been pouring over the source code of express, socket.io-node, hummingbird and many other applications. Especially lookout for TJ Holowaychuk, Guillermo Rauch, Isaacs, ry.<p>- the #node.js channel on irc is just amazing. There are interesting conversations, and I have had a lot of help regarding not just problems I've run into, but also style. I've pasted bits of code, that looked ugly to me and others have helped me think through it.<p>Hope this helps a bit.<p>Of course look at all the great guides and books about node.js such as howtonode.org , mastering node and others. There is a good list on: <a href="https://github.com/joyent/node/wiki/Resources" rel="nofollow">https://github.com/joyent/node/wiki/Resources</a>
评论 #2539236 未加载
评论 #2539205 未加载
ricardobeat大约 14 年前
I've worked on a single, medium-size app to date, but I think these might have an impact:<p>- try Coffeescript. It may look strange at first but ends up way more elegant and readable<p>- use [Step](<a href="https://github.com/creationix/step" rel="nofollow">https://github.com/creationix/step</a>) to ease sequential async calls<p>- visually organise your code (formatting, lined up declarations, well separated pieces of functionality). This can help a lot after you've moved on from one part of the codebase to another
mparke大约 14 年前
Question, How many modules is your server broken up into? Are you utilizing exports?
评论 #2539159 未加载