2019-08-02

2019-08-02

[2019-08-31]mac下安装anaconda之后需要配置环境变量
[2019-08-13]npm script传参
[2019-08-04]js生成随机字符串
[2019-08-02]小程序更新api使用
[2019-08-02]让Nodejs请求是绕开本地hosts文件(如mac/linunx下/etc/hosts)

[2019-08-31]mac下安装anaconda之后需要配置环境变量

在网上看到很多anaconda的安装使用教程, 都是说下载安装包, 安装完成之后即可在终端使用

但我在实际安装时却没办法直接使用, 可能因为我用的是iterm而不是默认终端

这个时候其实只要检查下anaconda的安装目录, 然后设置一下环境变量即可

# 因为我使用zsh, 所以是在~/.zshrc中修改
export PATH=/anaconda3/bin:$PATH
[2019-08-13]npm script传参

来自这个回答 https://cloud.tencent.com/developer/ask/50047

这里简单做下记录

语法如下:

npm run <command> [-- <args>]

注意必要的–,需要将参数传递到npm命令本身,并将其传递给脚本。

假设你有package.json

"scripts": {
    "grunt": "grunt",
    "server": "node server.js"
}

那么相当于

grunt task:target

通过npm运行

npm run grunt -- task:target

相当于

node server.js --port=1337

然后会变成

npm run server -- --port=1337

你可以通过以下方法来解决:

假设你的package.json包名是myPackage:

"scripts": {
    "start": "node ./script.js server"
}

然后加上package.json:

"config": {
    "myPort": "8080"
}

在你的script.js中:

// defaulting to 8080 in case if script invoked not via "npm run-script" but directly
var port = process.env.npm_package_config_myPort || 8080

那么在默认情况下npm start将会使用8080。但你可以按照以下的方式来配置它(该值将通过npm存储在其内部存储器中):

npm config set myPackage:myPort 9090

然后,在调用npm start时,将使用9090(来自package.json的默认值将被覆盖)。

[2019-08-04]js生成随机字符串

from https://www.cnblogs.com/makan/p/4850071.html

Math.random().toString(36).substr(2);
[2019-08-02]小程序更新api使用

来自《小程序的强制更新机制》

// wx.getUpdateManager 在 1.9.90 才可用,请注意兼容
const updateManager = wx.getUpdateManager()
 
updateManager.onCheckForUpdate(function (res) {
  // 请求完新版本信息的回调
  console.log(res.hasUpdate)
})
 
updateManager.onUpdateReady(function () {
  wx.showModal({
    title: '更新提示',
    content: '新版本已经准备好,是否马上重启小程序?',
    success: function (res) {
      if (res.confirm) {
        // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
        updateManager.applyUpdate()
      }
    }
  })
})
 
updateManager.onUpdateFailed(function () {
  // 新的版本下载失败
})
[2019-08-02]让Nodejs请求是绕开本地hosts文件(如mac/linunx下/etc/hosts)

from nodejs-bypass-linux-hosts-file

注意: 未在windows上进行测试

$ cat /etc/hosts
<snip>
127.0.0.1 google.com
$ ping google.com
PING google.com (127.0.0.1) 56(84) bytes of data.
const dns = require('dns')

// This will use the hosts file.
dns.lookup('google.com', function (err, res) {
  console.log('This computer thinks Google is located at: ' + res)
})

dns.resolve4('google.com', function (err, res) {
  console.log('A real IP address of Google is: ' + res[0])
})