// 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
// 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
| ├── index.js
| └── server.js
├── product
| ├── index.js
| ├── product.js
| ├── product.spec.js
| └── product.hbs
.
├── 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();