發表文章

目前顯示的是有「nodejs」標籤的文章

[Nodejs] npm install 時跑出錯誤

在 Ubuntu 20.04, Node version: 16.13.0 在安裝 opecc 出噴出一堆錯誤,原本以為是 npm WARN deprecated node-pre-gyp@0.14.0: Please upgrade to @mapbox/node-pre-gyp: the non-scoped node-pre-gyp package is deprecated and only the @mapbox scoped package will recieve updates in the future 這個警告造成無法安裝,但更新之後一樣無法安裝 最好詳細追查錯誤 log 看到底下: 1555 error /bin/sh: 1: python: not found 大致猜測是 20.04 預設沒有 python, 而是 python3 所以用 ln -s /usr/bin/python3 /usr/bin/python 之後 重新執行 npm i 就可以安裝囉!

[Node.js] require 的機制

最近在面試的時候, 寫了一些基本的題目, 但是卻有些地方是我自以為清楚, 但是不清楚的地方, 那就是這次要寫的 require! 其實一般人使用應該都會知道 require 完後, Node.js 會把 require 的物件, 會快取在記憶體內, 但是由於我真的太久沒寫, 所以在沒有理解的情況下, 也會忘記是不是真的每次 require 後就會存在記憶體中, 所以這次來分析一下原始碼 https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js // Loads a module at the given file path. Returns that module's // `exports` property. Module.prototype.require = function(id) { validateString(id, 'id'); if (id === '') { throw new ERR_INVALID_ARG_VALUE('id', id, 'must be a non-empty string'); } requireDepth++; try { return Module._load(id, this, /* isMain */ false); } finally { requireDepth--; } }; 從上面就可以看出, 關鍵是在 Module._load 那我們再看一下 Module._load 的程式碼 // Check the cache for the requested file. // 1. 假如一個 module 已經存在快取中, 直接返回其 exports 的物件 // 2. 假如 module 是原生系統內部的 module (ex: file, path), // 呼叫 NativeModule.prototype.compileForPublicLoader() 並且返回其 exports...

Node.js 淺談筆記

Embarrassingly parallel 透過 manager process 分配工作給 replica process 經典代表: php 看起來很美好, 但有些問題 在做 CPU 密集的工作或是跟IO相關的工作時 該 replica process 就會整個 idle CPU如果一直在做 context switch 其實也會造成延遲 所以並不是說 worker process 開得越多越好 所以通常都會開核心數的 2 ~ 4 倍的 worker process 來達到充份利用CPU 例如: 4 核心可能會需要開 8 ~ 16 個 worker process 超過這個範圍, 效能不會增加, 反而會下降 因為都會浪費時間在 context switch 上 Node.js 也是類似的方式, 但跟PHP不同的是 Node.js 本身在執行Javascript的部分是單執行緒(single thread), 透過 event queue 來壓榨單顆核心的效能 如果每個 process 都是單執行緒, 那就開核心數個 process ex: 4 核心就開 4 個 process, 透過這種方法把所有核心榨乾 但是還是有缺點 1. 沒有共享記憶體 (shared memory) 2. 如果有用 connection pool 的機制, connection pool 數會是一般的核心數倍 ex: node.js 連接 redis 時假設 connection pool 數是設定 20條 connections 4 cores 就會有 20*4 條 connections 有可能會造成很多 idle connections 像是 postgresql 每條 connection 都是一個 process 彼此透過 shared memory 如果某個 process 掛掉了, 不會影響到其他 process Node.js 也是有類似的好處 一個 process 掛了不影響到其他 process (Node.js 每個 process有自己的 event queue & connection pool) Java, Golang 每個 process 會開很多個 th...

[Node.js] 進階 (20%完成)

圖片
Node.js 靠 process.binding('xx') 使用 C++ 函數 Node.js 內包含 .cc (C++ code) 像這次範例是 PBKDF2 (用來做crypto相關的) 在C++ function 內要給 process.binding()用的話就要透過 env->SetMethod(target, "PBKDF2", PBKDF2) V8引擎則是把javascript裡的一些東西轉成C++看得懂的東西 ex: V8:Array => 把js中的array轉成C++可以用的code Thread 當我們在電腦中執行程式的時候, 電腦會起一個 process 一個process是程式本身的實例 一個process內可以有多個threads 一個thread可以看成一個to-do list 要被CPU執行的指令集 os scheduler來排定哪個thread要優先執行 一個核心一次可以處理超過一個thread (multi-thread) Recap: Thread是指令集的單位, 等著被CPU執行 Event Loop 詳細版:  http://www.eebreakdown.com/2016/09/nodejs-eventemitter.html 用來處理非同步的事件 Node.js執行之後就直接進event loop 整個event loop從頭跑到尾是一次是一個tick shouldContinue 是 event loop 判斷是不是還有事件可以繼續跑 pendingOSTasks => networking related pendingOperations => file system related 如果要讓node.js的middleware在執行完原本的handler function後才執行 可以用這個方式 此處的用法是, 在新增完部落格的文章之後, 才清除文章快取 # CI/CD Travis .travis.yml language 使用的城市語言 node_js - 8 => 使用v8.x dist...

[PHP][Node.js] 指定特定的network interface送出請求

Linux # curl --interface eth0:1 PHP $url = "http://www.google.com"; $curlh = curl_init($url); curl_setopt($curlh, CURLOPT_USERAGENT, $uagent); curl_setopt($curlh, CURLOPT_INTERFACE, “888.888.888.888“); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curlh); Ref:  http://blog.unethost.com/php_assign_outgoing_ip_address/ Node.js 使用http.request中 , options可以帶 localAddress 也就是主機上的ip or npm request module, options也可以帶 localAddress Ref:  https://stackoverflow.com/questions/15773672/node-js-http-request-using-network-interface

[ Node.js ] node-gyp rebuild 問題

最近因為一直使用 v7.10.1 在使用某個 module 的時候, npm install 會失敗 並且跳出要執行 node-gyp rebuild 的訊息 但是輸入之後一樣會失敗 以下是我自己的解決方法 先確定本機端是否有 /usr/bin/python, 且 python是要2.7版本的 如果沒有 python2.7的話, 可以 apt-get install python2.7 ln -s /usr/bin/python2.7 /usr/bin/python 以上的用好之後, 直接重新執行 npm install 然後再執行 node-gyp rebuild就可以囉

[ Node.js ] winstonjs rangeError Maximum call stack size exceeded

Reference:  https://github.com/winstonjs/winston/issues/862 在 winston/common.js 第217行 加入 options.meta = meta; https://github.com/winstonjs/winston/blob/master/lib/winston/common.js#L217 // Remark: this should really be a call to `util.format`. // if ( typeof options . formatter == ' function ' ) { options . meta = meta; return String ( options . formatter ( exports . clone (options))); }

[ Nodejs ] Module did not self-register

之前使用nvm時突然發現原本可以work的程式一直噴錯: Module did not self-register

[ NodeJS ] NodeJS + nginx 設置

工欲善其事,必先利其器 我們的環境使用Amazon EC2 的Ubuntu

[ AWS ] 在AWS Ubuntu上安裝vsftpd & nodejs環境

圖片
Reference : AWS inital config http://40era.com/1572/ Using Filezilla http://www.codestore.net/store.nsf/unid/BLOG-20111012-0812 Install vsftpd http://www.webteach.tw/?p=76 Install nodejs http://therightchoyce.tumblr.com/post/37209565860/installing-nodejs-on-an-ec2-ubuntu-instance-for https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions install nodejs , but it doesn't work http://stackoverflow.com/questions/26396529/node-js-doesnt-work-on-amazon-ec2-ubuntu-14-04-server http://stackoverflow.com/questions/26320901/cannot-install-nodejs-usr-bin-env-node-no-such-file-or-directory