Node.js learning Notes
Installation:
nodejs.org/zh-cn/ Check whether the installation is successful :node -v
Run:
- Vscode: Click the debug icon on the left and select running platform Node
- Vscode: Install the run coder plug-in, run a JS file right click run Code
Node related Tools
1: Node Version Manager (NVM)
- Mac installation: installation address
Git installation: ) 'CD ~/' git clone https://github.com/nvm-sh/nvm.git. NVM 'CD ~/. NVM' and checkout the latest version 'git checkout v0.38.0' Activate NVM by getting it from the shell: '.. /nvm.sh 'Now add these lines to your ~/.bashrc, ~/.profile, or ~/.zshrc files for automatic access at login: (You may need to add to more than one of the above files) 'CD ~/' from this place, Ls -a export NVM_DIR="$HOME/. NVM "[-s "$NVM_DIR/nvm.sh"] && \. "$NVM_DIR/nvm.sh" # This loads NVM [ -s "$NVM_DIR/bash_completion"] && \. "\$NVM_DIR/bash_completion" # This loads NVM bash_completion ' 'command -v NVM' : Output: NVMCopy the code
- Windows installation:
nvm-windows
nodist
Copy the code
2: NPM (Node Package Manager)
- Global install package command:
'NPM install forever --global (-g)' 'forever' 'NPM uninstall forever --global' 'forever'Copy the code
- Global installation directory
Mac: / Users/felix/NVM/versions/bin/node/NVM each version.
Windows: C :\users\ Users \appData\Roaming\ NPM \node_modules
Simple tutorial:
1. Warm up
(1) Request data
Ajax example:
const xhr = new XMLHttpRequest(); xhr.open('get', 'https://aldh5.tmall.com/recommend2.htm?&appId=201602265, 201602267070, 55201022, 66201030, 118090, 44201051, 70201122, 21 201606279201062 & 710 callback = jsonp_31960259 '); xhr.send() <! -- Run: NPX http-server -->Copy the code
Server. Js sample:
const http = require('https') HTTP get (' https://aldh5.tmall.com/recommend2.htm?&appId=201602265, 201602267070, 55201022, 66201030, 118090, 44201051, 70, 2 , 01612221201062, 79201062, 710 & callback = jsonp_31960259 ', (res) = > {let STR = 'res. On (' data', (thunk) = > {/ / collecting flow STR + = thunk}) res. On (' end '() = > {/ / over the console log (STR)})}) / / Run: right-click the Run CodeCopy the code
(2) File system
const fs = require('fs'); Fs.writefile ('./log.txt', "hello", (err, data) => {if (err) {} else {}}Copy the code
(3) Process management
Console. log(process.argv.slice(2)) // Run: at the previous level the file enters the terminal: node index.js argv1 argv2Copy the code
(4) Network communication (HTTP/HTTPS)
Const HTTP = require(' HTTP ') const server = http.createserver ((request, response) => {const HTTP = require(' HTTP ') const server = http.createserver ((request, response) => { Let url = request.url; Response.write (url) response.end()}) server.listen(8090, 'localhost', () => {console.log('localhost:8090')}) Right click the Run CodeCopy the code
2. NPM initializes, installs, and views dependencies
(1) Initialize and install view dependencies
Role command | | | | -- - | -- - | | NPM init - initialization y | | | | show depend on the structure of NPM list | | NPM list | grep XX XX branch | view dependent structure | | NPM I XX -s | production installation XX rely on | | NPM I XX - D | development environment installed XX rely on | | NPM XX XX rely on | | uninstall uninstall | NPM I - production | testers need to install a production environment bag | | NPM view XX Version | see XX rely on version | | NPM XX @ version - I S | production to install a version of the XX rely on, Don't write the version number is to install the latest | | NPM cache clean -- force | clear the cache | | NPM outdated | check outdated packages, have no have no output |Copy the code
(2) Symbol before NPM version number: symbol + Major version number (Minor) + Patch, column: ^13.0.6
| symbol function | | | -- - | -- - | | | locking patch | | ^ | lock major | | to | locking minor | | * | | the latest versionCopy the code
3. Develop the simplest package you can
-
Create “app.js” under “01-Buildings” and its code:
//const path = require('path') //console.log(path.resolve(__dirname,'.. /')) //__dirname =require('lodash') function mychunk(arr){let arr2=_.chunk(arr,2) return arr2} module.exports=mychunkCopy the code
, index-text.js, its code:
Const mychunk =require('./app.js') console.log(mychunk([2,3,4,5])Copy the code
01- Buildings under terminal command:
npm init -y ; npm install lodash; Copy the code
-
Create NPM user from NPM official website, then run terminal command under 01-buildings:
NPM adduser Indicates the associated user NPM publishCopy the code
Graph TD Create NPM user --> Terminal associate user --> Terminal publish
Source of 4.
4.1 Manual Switchover
View the current source: NPM config switch get registry taobao source: NPM config set registry at https://registry.npm.taobao.orgCopy the code
4.2 NRM Manages sources
NRM (NPM Registry Manager) is the mirror source management tool of NPM. Sometimes foreign resources are too slow. It can be used to quickly switch between NPM sources.
2 install the NRM
Run the NPM install -g NRM command to install NRM globally
4.2.2 use NRM
Run the NRM is command to view available sources. The source marked with * is the current source. The preceding output indicates that the current source is the official source
Switch holdings NRM
Switch to the Taobao source and run the NRM use Taobao command
5. NPM script
5.1 Naming:
(1) Custom naming, example XX –> run time, NPM run XX
(2) specify the name: start, test, dev, eject, build –> run can be omitted
5.2 Script Running
5.2.1 The simplest script example
In package.json scripts, configure, “XX”:”echo hello”, run NPM run XX to run XX configuration, here run terminal from 02-npm file, call greeting configuration to print Hello
5.2.2 Upgrade Scripts
- Base: xx.js file terminal command execution:
node XX.js
That is, run the xx.js file
- Upgrade: In package.json scripts, configure,
"XX":"node js1 path & node js2 path etc
“(serial running, random order), terminal running:npm run XX
, that is, run js1 file, js2 file..
5.3 variable
One very powerful feature of NPM scripts is the ability to use NPM internal variables.
First, the NPM script can retrieve the fields in package.json with the npm_package_ prefix. For example, here is a package.json.
! Note that it must be run in an NPM script (e.g., NPM run view). Running js directly from the command line (e.g., node view.js) will not get the value
Example error :(via xx.js)
Correct example 1 :(via xx.js)
Correct example 2: (through configuration), add :”env” :”env”, run to list all environment variables
5.4 NPM Install git packages
- Suitable for installing projects on your company’s internal Git server
npm install git+https://[email protected]:xyy-github/react_note.git
- Or SSH
npm install git+ssh://[email protected]:xyy-github/react_note.git
5.5 cross – env
5.5.1 What is cross-env?
Run cross-platform setup scripts that use environment variables
5.5.2 Causes
Most Windows command prompts will block with an error when you use NODE ENV = production to set environment variables. The exception is Bash on Windows, which uses native Bash. In other words, Windows does not support the NoDE env-production setup
5.5.3 solve
Cross-env lets you use a single command without having to worry about setting up or using environment variables correctly for the platform. This mini-package (cross-env) provides a script for setting environment variables, allowing you to set environment variables in Unix mode and then run compatible on Windows.
5.5.4 installation
npm install --save-dev cross-env