最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - JavaScript naming conventions when working with nodejs - Stack Overflow

programmeradmin2浏览0评论

I'm working a lot more with JavaScript in node.js. My application has the following general 'classes' that are used.

Server side:

  • Libraries
  • Models
  • Utilities
  • Routes

Client side (backbone.js):

  • Views
  • Models
  • Collections

The client side is pretty straight forward. I name all files related to what they are, such as UserModel.js, UserView.js, UserCollection.js, etc.

The server-side however, gets more messy. For example:

Models are related to MongoDB collections. Each Model is just a wrapper for various functionality. If I have a users collection, I have a collection called users, my model is Users.js.

Libraries I have for example Users.js as well, that interacts with the model and contains the majority of logic.

However, this really shouldn't be called Users, mainly because I'm getting confused now.

Routes are simply related to the URL. So if you have /account/ I would have an account.js route - all lowercase.

Utilities - I just have one util.js that I don't use much so I'm less concerned and the naming seems fine for it's purpose and size.

How would you suggest naming things that are sort of generic like "Libraries", that differentiates them from Models/Routes.

I'm working a lot more with JavaScript in node.js. My application has the following general 'classes' that are used.

Server side:

  • Libraries
  • Models
  • Utilities
  • Routes

Client side (backbone.js):

  • Views
  • Models
  • Collections

The client side is pretty straight forward. I name all files related to what they are, such as UserModel.js, UserView.js, UserCollection.js, etc.

The server-side however, gets more messy. For example:

Models are related to MongoDB collections. Each Model is just a wrapper for various functionality. If I have a users collection, I have a collection called users, my model is Users.js.

Libraries I have for example Users.js as well, that interacts with the model and contains the majority of logic.

However, this really shouldn't be called Users, mainly because I'm getting confused now.

Routes are simply related to the URL. So if you have /account/ I would have an account.js route - all lowercase.

Utilities - I just have one util.js that I don't use much so I'm less concerned and the naming seems fine for it's purpose and size.

How would you suggest naming things that are sort of generic like "Libraries", that differentiates them from Models/Routes.

Share Improve this question asked Apr 25, 2012 at 23:21 dzmdzm 23.6k51 gold badges152 silver badges229 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

The most important thing in your naming convention is consistency. You can figure out pretty much any naming convention as long as it is sane and consistent.

That being said, I would probably be more verbose in my names in this case. Paths might be good enough, but I would rather see UserRoutes.js, UserModel.js and maybe even UserLib.js based on your examples.

In some of my node.js projects, I have even taken to not using a .js extension. My routes for instance would be user.routes. It is easy enough to change the syntax highlighting in editors based on different extensions.

Helpers, extensions, generics, logic, repository, managers, mediator, municator...any of those?

I usually name things based on the design pattern I use to implement them.

I actually do sort of what you're doing now. I have folders for models, routes, views, and controllers. I then have a file called user.js in each folder. When I need to fix a bug or implement something to do with users, it's pretty easy to figure out where I need to go.

I think it would be confusing if I tried to e up with clever names for all of these files when they are all implementing different aspects of a related thing.

发布评论

评论列表(0)

  1. 暂无评论