我在閱讀 NodeJS 文檔中讀出的 19 個套路

wangqi1990 8年前發布 | 38K 次閱讀 Node.js 操作系統 Node.js擴展

雖然我已經用了三年多的NodeJS,也曾經以為自己對其無所不知。但是我好像從未有安靜的坐下來仔細地閱讀NodeJS的完整文檔。如果有熟悉我的朋友應該知道,我之前已經看了HTML,DOM,Web APIs,CSS,SVG以及ECMAScript的文檔,NodeJS是我這個系列的最后一個待翻閱的山峰。在閱讀文檔的過程中我也發現了很多本來不知道的知識,我覺得我有必要分享給大家。不過文檔更多的是平鋪直敘,因此我也以閱讀的順序列舉出我覺得需要了解的點。

querystring:可以用作通用解析器的模塊

很多時候我們會從數據庫或其他地方得到這種奇怪格式的字符串: name:Sophie;shape:fox;condition:new ,一般來說我們會利用字符串切割的方式來講字符串劃分到JavaScript Object。不過 querystring 也是個不錯的現成的工具:

const weirdoString = `name:Sophie;shape:fox;condition:new`;
const result = querystring.parse(weirdoString, `;`, `:`);
// result:
// {
//   name: `Sophie`,
//   shape: `fox`,
//   condition: `new`,
// };

V8 Inspector

以 --inspect 參數運行你的Node應用程序,它會反饋你某個URL。將該URL復制到Chrome中并打開,你就可以使用Chrome DevTools來調試你的Node應用程序啦。不過需要注意的是,該參數仍然屬于實驗性質。

nextTick 與 setImmediate的區別

這兩貨的區別可能光從名字上還看不出來,我覺得應該給它們取個別名:

  • process.nextTick() 應該為 process.sendThisToTheStartOfTheQueue()

  • setImmediate 應該為 sendThisToTheEndOfTheQueue()

再說句不相關的,React中的Props應該為 stuffThatShouldStayTheSameIfTheUserRefreshes ,而State應該為 stuffThatShouldBeForgottenIfTheUserRefreshes 。

Server.listen 可以使用Object作為參數

我更喜歡命名參數的方式調用函數,這樣相較于僅按照順序的無命名參數法會更直觀。別忘了Server.listen也可以使用某個Object作為參數:

require(`http`)
  .createServer()
  .listen({
    port: 8080,
    host: `localhost`,
  })
  .on(`request`, (req, res) => {
    res.end(`Hello World!`);
  });

不過這個特性不是表述在 http.Server 這個API中,而是在其父級 net.Server 的文檔中。

相對地址

你傳入 fs 模塊的距離可以是相對地址,即相對于 process.cwd() 。估計有些人早就知道了,不過我之前一直以為是只能使用絕對地址:

const fs = require(`fs`);
const path = require(`path`);
// why have I always done this...
fs.readFile(path.join(__dirname, `myFile.txt`), (err, data) => {
  // do something
});
// when I could just do this?
fs.readFile(`./path/to/myFile.txt`, (err, data) => {
  // do something
});

Path Parsing :路徑解析

之前我一直不知道的某個功能就是從某個文件名中解析出路徑,文件名,文件擴展等等:

myFilePath = `/someDir/someFile.json`;
path.parse(myFilePath).base === `someFile.json`; // true
path.parse(myFilePath).name === `someFile`; // true
path.parse(myFilePath).ext === `.json`; // true

Logging with colors

別忘了 console.dir(obj,{colors:true}) 能夠以不同的色彩打印出鍵與值,這一點會大大增加日志的可讀性。

使用setInterval執行定時任務

我喜歡使用 setInterval 來定期執行數據庫清理任務,不過默認情況下在存在 setInterval 的時候NodeJS并不會退出,你可以使用如下的方法讓Node沉睡:

const dailyCleanup = setInterval(() => {
  cleanup();
}, 1000 * 60 * 60 * 24);
dailyCleanup.unref();

Use Signal Constants

如果你嘗試在NodeJS中殺死某個進程,估計你用過如下語法:

process.kill(process.pid, `SIGTERM`);

這個沒啥問題,不過既然第二個參數同時能夠使用字符串與整形變量,那么還不如使用全局變量呢:

process.kill(process.pid, os.constants.signals.SIGTERM);

IP Address Validation

NodeJS中含有內置的IP地址校驗工具,這一點可以免得你寫額外的正則表達式:

require(`net`).isIP(`10.0.0.1`) 返回 4
require(`net`).isIP(`cats`) 返回 0

os.EOF

不知道你有沒有手寫過行結束符,看上去可不漂亮啊。NodeJS內置了 os.EOF ,其在Windows下是 rn ,其他地方是 n , 使用os.EOL 能夠讓你的代碼在不同的操作系統上保證一致性:

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
  });
});

HTTP 狀態碼

NodeJS幫我們內置了HTTP狀態碼及其描述,也就是 http.STATUS_CODES ,鍵為狀態值,值為描述:

你可以按照如下方法使用:

someResponse.code === 301; // true
require(`http`).STATUS_CODES[someResponse.code] === `Moved Permanently`; // true

避免異常崩潰

有時候碰到如下這種導致服務端崩潰的情況還是挺無奈的:

const jsonData = getDataFromSomeApi(); // But oh no, bad data!
const data = JSON.parse(jsonData); // Loud crashing noise.

我為了避免這種情況,在全局加上了一個:

process.on(`uncaughtException`, console.error);

當然,這種辦法絕不是 最佳實踐 ,如果是在大型項目中我還是會使用 PM2 ,然后將所有可能崩潰的代碼加入到 try...catch 中。

Just this once()

除了 on 方法, once 方法也適用于所有的EventEmitters,希望我不是最后才知道這個的:

server.once(`request`, (req, res) => res.end(`No more from me.`));

Custom Console

你可以使用 new console.Console(standardOut,errorOut) ,然后設置自定義的輸出流。你可以選擇創建console將數據輸出到文件或者Socket或者第三方中。

DNS lookup

某個年輕人告訴我,Node 并不會緩存DNS查詢信息 ,因此你在使用URL之后要等個幾毫秒才能獲取到數據。不過其實你可以使用 dns.lookup() 來緩存數據:

dns.lookup(`www.myApi.com`, 4, (err, address) => {
  cacheThisForLater(address);
});

fs 在不同OS上有一定差異

  • fs.stats() 返回的對象中的 mode 屬性在Windows與其他操作系統中存在差異。

  • fs.lchmod() 僅在macOS中有效。

  • 僅在Windows中支持調用 fs.symlink() 時使用 type 參數。

  • 僅僅在macOS與Windows中調用 fs.watch() 時傳入 recursive 選項。

  • 在Linux與Windows中 fs.watch() 的回調可以傳入某個文件名

  • 使用 fs.open() 以及 a+ 屬性打開某個目錄時僅僅在FreeBSD以及Windows上起作用,在macOS以及Linux上則存在問題。

  • 在Linux下以追加模式打開某個文件時,傳入到 fs.write() 的 position 參數會被忽略。

net 模塊差不多比http快上兩倍

筆者在文檔中看到一些關于二者性能的討論,還特地運行了兩個服務器來進行真實比較。結果來看 http.Server 大概每秒可以接入3400個請求,而 net.Server 可以接入大概5500個請求。

// This makes two connections, one to a tcp server, one to an http server (both in server.js)
// It fires off a bunch of connections and times the response

// Both send strings.

const net = require(`net`);
const http = require(`http`);

function parseIncomingMessage(res) {
  return new Promise((resolve) => {
    let data = ``;

    res.on(`data`, (chunk) => {
      data += chunk;
    });

    res.on(`end`, () => resolve(data));
  });
}

const testLimit = 5000;


/*  ------------------  */
/*  --  NET client  --  */
/*  ------------------  */
function testNetClient() {
  const netTest = {
    startTime: process.hrtime(),
    responseCount: 0,
    testCount: 0,
    payloadData: {
      type: `millipede`,
      feet: 100,
      test: 0,
    },
  };

  function handleSocketConnect() {
    netTest.payloadData.test++;
    netTest.payloadData.feet++;

    const payload = JSON.stringify(netTest.payloadData);

    this.end(payload, `utf8`);
  }

  function handleSocketData() {
    netTest.responseCount++;

    if (netTest.responseCount === testLimit) {
      const hrDiff = process.hrtime(netTest.startTime);
      const elapsedTime = hrDiff[0] * 1e3 + hrDiff[1] / 1e6;
      const requestsPerSecond = (testLimit / (elapsedTime / 1000)).toLocaleString();

      console.info(`net.Server handled an average of ${requestsPerSecond} requests per second.`);
    }
  }

  while (netTest.testCount < testLimit) {
    netTest.testCount++;
    const socket = net.connect(8888, handleSocketConnect);
    socket.on(`data`, handleSocketData);
  }
}


/*  -------------------  */
/*  --  HTTP client  --  */
/*  -------------------  */
function testHttpClient() {
  const httpTest = {
    startTime: process.hrtime(),
    responseCount: 0,
    testCount: 0,
  };

  const payloadData = {
    type: `centipede`,
    feet: 100,
    test: 0,
  };

  const options = {
    hostname: `localhost`,
    port: 8080,
    method: `POST`,
    headers: {
      'Content-Type': `application/x-www-form-urlencoded`,
    },
  };

  function handleResponse(res) {
    parseIncomingMessage(res).then(() => {
      httpTest.responseCount++;

      if (httpTest.responseCount === testLimit) {
        const hrDiff = process.hrtime(httpTest.startTime);
        const elapsedTime = hrDiff[0] * 1e3 + hrDiff[1] / 1e6;
        const requestsPerSecond = (testLimit / (elapsedTime / 1000)).toLocaleString();

        console.info(`http.Server handled an average of ${requestsPerSecond} requests per second.`);
      }
    });
  }

  while (httpTest.testCount < testLimit) {
    httpTest.testCount++;
    payloadData.test = httpTest.testCount;
    payloadData.feet++;

    const payload = JSON.stringify(payloadData);

    options[`Content-Length`] = Buffer.byteLength(payload);

    const req = http.request(options, handleResponse);
    req.end(payload);
  }
}

/*  --  Start tests  --  */
// flip these occasionally to ensure there's no bias based on order
setTimeout(() => {
  console.info(`Starting testNetClient()`);
  testNetClient();
}, 50);

setTimeout(() => {
  console.info(`Starting testHttpClient()`);
  testHttpClient();
}, 2000);
// This sets up two servers. A TCP and an HTTP one.
// For each response, it parses the received string as JSON, converts that object and returns a string
const net = require(`net`);
const http = require(`http`);

function renderAnimalString(jsonString) {
  const data = JSON.parse(jsonString);
  return `${data.test}: your are a ${data.type} and you have ${data.feet} feet.`;
}


/*  ------------------  */
/*  --  NET server  --  */
/*  ------------------  */

net
  .createServer((socket) => {
    socket.on(`data`, (jsonString) => {
      socket.end(renderAnimalString(jsonString));
    });
  })
  .listen(8888);


/*  -------------------  */
/*  --  HTTP server  --  */
/*  -------------------  */

function parseIncomingMessage(res) {
  return new Promise((resolve) => {
    let data = ``;

    res.on(`data`, (chunk) => {
      data += chunk;
    });

    res.on(`end`, () => resolve(data));
  });
}

http
  .createServer()
  .listen(8080)
  .on(`request`, (req, res) => {
    parseIncomingMessage(req).then((jsonString) => {
      res.end(renderAnimalString(jsonString));
    });
  });

REPL tricks

  • 如果你是在REPL模式下,就是直接輸入node然后進入交互狀態的模式。你可以直接輸入 .load someFile.js 然后可以載入包含自定義常量的文件。

  • 可以通過設置 NODE_REPL_HISTORY="" 來避免將日志寫入到文件中。

  • _ 用來記錄最后一個計算值。

  • 在REPL啟動之后,所有的模塊都已經直接加載成功。可以使用 os.arch() 而不是 require( os ).arch() 來使用。

 

來自:https://segmentfault.com/a/1190000007435273

 

 本文由用戶 wangqi1990 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!