如何高效的构建nodejs项目
2016-12-20 15:06
295 查看
The 5 fundamental rules of a Node.js Project Structure
There are a lot of possible ways to organize a Node.js project - and each of the known methods has their ups and downs. However, according to our experience, developers always want to achieve the same things: clean code and the possibility of adding new featureswith ease.
In the past years at RisingStack, we had a chance to build efficient Node applications in many sizes, and we gained numerous insights regarding the dos and donts of project structuring.
We have outlined five simple guiding rules which we enforce during Node.js development. If you manage to follow them, your projects will be fine:
Rule 1 - Organize your Files Around Features, Not Roles
Imagine, that you have the following directory structure:// DON'T . ├── controllers | ├── product.js | └── user.js ├── models | ├── product.js | └── user.js ├── views | ├── product.hbs | └── user.hbs
The problems with this approach are:
to understand how the product pages work, you have to open up three different directories, with lots of context switching,
you end up writing long paths when requiring modules:
require('../../controllers/user.js')
"Rule
1: Organize your files around features, not roles!" via @risingstack
CLICK
TO TWEET
Instead of this, you can structure your Node.js applications around product features / pages / components. It makes understanding a lot easier:
// DO . ├── product | ├── index.js | ├── product.js | └── product.hbs ├── user | ├── index.js | ├── user.js | └── user.hbs
See
the communication between your Node.js services - check out Trace by RisingStack!
Rule 2 - Don't Put Logic in index.js
Files
Use these files only to export functionality, like:// product/index.js var product = require('./product') module.exports = { create: product.create }
Rule 3 - Place Your Test Files Next to The Implementation
Tests are not just for checking whether a module produces the expected output, they also document your modules (you will learn more on testing in the upcoming chapters). Because of this, it is easier to understand if test files are placed next to theimplementation.
"Rule
3: Place your test files next to the implementation." via @risingstack
CLICK
TO TWEET
Put your additional test files to a separate
testfolder
to avoid confusion.
. ├── test | └── setup.spec.js ├── product | ├── index.js | ├── product.js | ├── product.spec.js | └── product.hbs ├── user | ├── index.js | ├── user.js | ├── user.spec.js | └── user.hbs
Rule 4 - Use a config
Directory
To place your configuration files, use a configdirectory.
. ├── config | ├── index.js | └── server.js ├── product | ├── index.js | ├── product.js | ├── product.spec.js | └── product.hbs
Rule 5 - Put Your Long npm Scripts in a scripts
Directory
Create a separate directory for your additional long scripts in package.json. ├── scripts | ├── syncDb.sh | └── provision.sh ├── product | ├── index.js | ├── product.js | ├── product.spec.js | └── product.hbs
相关文章推荐
- RDC如何构建一个基于NodeJS的前后端项目
- 在大型Flex项目中如何构建高效的QTP测试框架
- 如何组织高效的项目会议
- 在TFS中构建适合自己的项目管理:如何在Team Project站点上增加自己的报表
- 在iOS项目构建时如何应用ccache
- C常见问题之如何构建一个完整的C项目
- 内容组织高效项目方案是如何产生的 !
- [问题]DotNet 项目如何实现在构建时 Build 号自动增加?
- Eclipse使用技巧:web项目构建时如何不删除WEB-INF/classes下的文件
- 如何构建高效的storm计算模型
- 开发那点事系列四 - 如何构建高效Ajax应用
- (一)如何使用gradle构建Android项目
- 如何高效开发三层架构项目
- 构建企业级项目管理体系(02)-如何做? 推荐
- 构建企业级项目管理体系(02)-如何做?
- 数据库日志清理杂谈--在项目中如何高效实现日志清理
- [转]如何使用国际开源项目构建一个完整的GIS(地理信息)应用系统
- 如何使用国际开源项目构建一个完整的GIS(地理信息)应用系统
- 用Maven如何构建一个基于Hibernate Annotation的web项目?
- 如何使用seajs+jQuery构建中型项目