Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Node.js 入门

管理安装在系统上的 Node.js

$ nvm ls-remote(查看远程所有版本)
$ nvm install v6.11.0(安装指定版本)
$ nvm install --lts(安装最新LTS版本)
$ nvm install node(安装最新版本)
$ nvm ls(列出安装的Node版本)
$ node -v
$ nvm use v6.11.0(切换当前使用版本)

创建 Node.js 项目

$ npm init(自定义设置)
$ pnm init -y(默认设置)
$ yarn init -y(使用yarn管理)

使用不同版本的 Node.js

$ nvm run v6.11.0 index.js(使用指定版本运行)
$ nvm exec v8.1.3 node -v

或使用nvm配置文件指定node版本:.nvmrc

.nvmrc文件内容:

8.1.3

测试:

$ nvm run node --version
$ nvm use

使用内置的 Node.js 模块

const os = require('os')

console.log(os.hostname())

安装第三方 Node.js 模块

$ npm i request --save
$ yar add request

使用第三方 Node.js 模块

const request = require('request')

request({
  url: 'https://api.douban.com/v2/movie/top250',
  json: true
}, (error, response, body) => {
  console.log(JSON.stringify(body, null, 2))
})

创建与使用自定义 Node.js 模块

创建:src/greeting.js

const hello = () => {
  console.log('hello ~')
}

module.exports.hello = hello

调用:index.js

const greeting = require('./src/greeting')
greeting.hello()

nodemon:监视应用的变化自动重启应用

$ npm i nodemon --save-dev
$ yar add nodemon --dev
$ ./node_modules/.bin/nodemon index.js

或者添加scripts

"scripts": {
  "start": "./node_modules/.bin/nodemon index.js"
}
$ npm run start
上一篇 Python实现Redis不同实例间数据迁移
下一篇 Node.js 测试