如何高效的构建nodejs项目
来源:互联网 发布:php 数组去除空值 编辑:程序博客网 时间:2024/05/17 22:44
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 features with 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.jsvar 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 the implementation.
"Rule 3: Place your test files next to the implementation." via @risingstack
CLICK TO TWEET
Put your additional test files to a separate test
folder 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 config
directory.
.├── 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
- 如何高效的构建nodejs项目
- RDC如何构建一个基于NodeJS的前后端项目
- nodejs构建项目
- 如何构建高效的storm计算模型
- 如何构建高效的storm计算模型
- 如何构建高效的MySQL分页
- 如何构建一个高效的敏捷团队
- 在大型Flex项目中如何构建高效的QTP测试框架
- NodeJS项目构建工具Grunt
- 解决Jenkins自动构建nodejs项目无法完成的问题
- 如何组织高效的项目会议
- 如何高效快速的项目开发
- 高效项目方案是如何产生的 !
- 如何高效的完成项目管理
- 浅谈IT企业如何构建高效运行的PMO
- 如何构建《健康,成长,快乐,高效》的研发团队
- 如何构建O2O场景,打造便捷高效的用户服务
- React+Webpack+Nodejs+Express快速构建项目
- Cookie和Session总结
- hive中order by,sort by, distribute by, cluster by作用以及用法
- 用libvlc进行网络串流streaming
- 台大机器学习——可行性证明1
- jQuery中标签的事件
- 如何高效的构建nodejs项目
- 米斯特白帽培训讲义 漏洞篇 代码执行
- Java设计模式一——策略模式
- 从Instant run谈Android替换Application和动态加载机制
- 设计模式——单例模式
- Java8新特性
- 关于全局变量的初始化问题与野指针
- VLC SDK在VS2010中的配置及简单使用举例
- codeforces 749C Voting