跳到主要内容

· 阅读需 10 分钟

十个 Node.js 的设计错误

Node.js 最初设计者 Ryan Dahl 2018 年在 JS Conf Berlin 的分享

沒有坚持使用 Promise

  • 在 2009 年六月在 Node 中开始引入 JavaScript 的 Promises,但又在 2010年二月就移除掉了。

    结果,随着日子久远,Node 里面就遍布着 async/await 和 promise 的不同 async API 设计,直至现时都极难整合

看轻了安全性(Security)

· 阅读需 1 分钟
const puppeteer = require('puppeteer');
const pageUrl = 'https://some-url.com';

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setRequestInterception(true);

page.on('request', (interceptedRequest) => {
// Don't intercept main document request
if (interceptedRequest.url === pageUrl) {
interceptedRequest.continue();
return;
}

// Intercept if request url starts with https
if (interceptedRequest.url.startsWith('https://')) {
interceptedRequest.continue({
// Replace https:// in url with http://
url: interceptedRequest.url.replace('https://', 'http://'),
});
return;
}

// Don't override other requests
interceptedRequest.continue();
})

await page.goto(pageUrl);
await browser.close();
})();

· 阅读需 2 分钟

今天在 git clone 一个 github 仓库的时候出现了以下错误

ssh_exchange_identification: read: Connection reset by peer

怀疑是代理的原因,试了全局、直连、规则都不行,查找了一些文章有说可以使用以下命令查看详细调试信息

ssh -vvv -T git@github.com

还是不行,在 v2ex 上看到一个楼主说需要加个配置,所以就试了下

~/.ssh/config 中添加以下配置

Host github.com
Hostname ssh.github.com
Port 443

再试下就可以了

· 阅读需 11 分钟

发现生产环境的业务报了好多错误, 涉及的 Node.js 代码是一个基于 Redis 的频率计数器,那部分逻辑大概是这样

// 查询并增加一次计数
async incr (id) {
const key = `${this.namespace}:${id}`
const now = getMicrotime()
const start = now - this.duration * 1000

const operations = [
['zremrangebyscore', key, 0, start],
['zcard', key],
['zadd', key, now, now],
['pexpire', key, this.duration]
]

const res = await this.redis.multi(operations).exec()
const count = toNumber(res[1][1])
return count
}

错误是:

Cannot read property '1' of undefined

· 阅读需 1 分钟

Mac 环境下 node 安装 canvas@2.6.1 出现以下错误时

node: cairo-pattern.c:1127: cairo_pattern_destroy: Assertion failed. none - catched error

使用 brew 安装一下以下几个库

brew install pixman cairo pango

不过你可能会遇到 python2.x 升级失败的问题

可以试试

brew uninstall python@2
brew install python
brew upgrade python

升级到 python3.x

来源: https://github.com/Automattic/node-canvas/issues/1065#issuecomment-373381272

· 阅读需 1 分钟
SELECT default_character_set_name FROM information_schema.SCHEMATA
WHERE schema_name = "schemaname";

SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "schemaname"
AND T.table_name = "tablename";

SELECT character_set_name FROM information_schema.`COLUMNS`
WHERE table_schema = "schemaname"
AND table_name = "tablename"
AND column_name = "columnname";