Skip to content

Latest commit

 

History

History
141 lines (132 loc) · 2.93 KB

NodeJS技巧.md

File metadata and controls

141 lines (132 loc) · 2.93 KB

Node项目目录结构最佳实践

  • 按照功能组织目录结构而不是规则
// DON'T
.
├── controllers
|   ├── product.js
|   └── user.js
├── models
|   ├── product.js
|   └── user.js
├── views
|   ├── product.hbs
|   └── user.hbs

// DO
.
├── product
|   ├── index.js
|   ├── product.js
|   └── product.hbs
├── user
|   ├── index.js
|   ├── user.js
|   └── user.hbs
  • 不要在index.js文件中写入业务逻辑
// product/index.js
var product = require('./product')

module.exports = {
  create: product.create
}
  • 测试文件应该放在它们的实现旁边
.
├── test
|   └── setup.spec.js
├── product
|   ├── index.js
|   ├── product.js
|   ├── product.spec.js
|   └── product.hbs
├── user
|   ├── index.js
|   ├── user.js
|   ├── user.spec.js
|   └── user.hbs
  • 使用 config目录
.
├── config
|   ├── index.js
|   └── server.js
├── product
|   ├── index.js
|   ├── product.js
|   ├── product.spec.js
|   └── product.hbs
  • 将你很长的 npm 脚本代码写入一个 脚本目录
.
├── scripts
|   ├── syncDb.sh
|   └── provision.sh
├── product
|   ├── index.js
|   ├── product.js
|   ├── product.spec.js
|   └── product.hbs
# 利用 querysString 切割文本
const weirdoString = `name:Sophie;shape:fox;condition:new`;
const result = querystring.parse(weirdoString, `;`, `:`);

// result:
// {
//   name: `Sophie`,
//   shape: `fox`,
//   condition: `new`,
// };
# 路径解析
myFilePath = `/someDir/someFile.json`;
path.parse(myFilePath).base === `someFile.json`; // true
path.parse(myFilePath).name === `someFile`; // true
path.parse(myFilePath).ext === `.json`; // true
# 结束符硬编码
const fs = require(`fs`);

// bad
fs.readFile(`./myFile.txt`, `utf8`, (err, data) => {
  data.split(`\r\n`).forEach(line => {
    // do something
  });
});

// good
const os = require(`os`);
fs.readFile(`./myFile.txt`, `utf8`, (err, data) => {
  data.split(os.EOL).forEach(line => {
    // do something
  });
});
# 状态码查询
someResponse.code === 301; // true
require(`http`).STATUS_CODES[someResponse.code] === `Moved Permanently`; // true
# 高精度时间
const NS_PER_SEC = 1e9;
const time = process.hrtime();
// [ 1800216, 25 ] 第一次返回当前的高精度时间

setTimeout(() => {
  const diff = process.hrtime(time);
  // [ 1, 552 ] 

  console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`);
  // benchmark took 1000000552 nanoseconds
}, 1000);
# 当无其他循环事件时推出 Timeout
const dailyCleanup = setInterval(() => {
  cleanup();
}, 1000 * 60 * 60 * 24);

dailyCleanup.unref();