北屋教程网

专注编程知识分享,从入门到精通的编程学习平台

Node.js 笔记_node.js教程详细

install

# 直接下载二进制文件
wget https://nodejs.org/dist/v12.13.1/node-v12.13.1-linux-x64.tar.xz

# .tar.xz 可直接解压
# tar -xvf node-v12.13.1-linux-x64.tar.xz

# 解压第一步
xz -d node-v12.13.1-linux-x64.tar.xz

# 解压第二步
tar -xvf node-v12.13.1-linux-x64.tar

# mv
mv node-v12.13.1-linux-x64 /usr/local/node

# ----------- export path 则不需要ln start
# node
ln -s /usr/local/node/bin/node /usr/bin/node

# npm
ln -s /usr/local/node/bin/npm /usr/bin/npm

# npx
ln -s /usr/local/node/bin/npx /usr/bin/npx

# ----------- export path 则不需要ln end

# vim /etc/profile
export PATH="$PATH:/usr/local/node/bin/"

# 立即生效
source /etc/profile

How to install Node.js via binary archive on Linux

How to install Node.js via binary archive on Linux[1]

step 1. Unzip the binary archive to any directory you wanna install Node, I use /usr/local/node

PROD_NODE_VERSION=v10.15.0
PROD_NODE_DISTRO=linux-x64
sudo mkdir -p /usr/local/node
sudo tar -xJvf node-$PROD_NODE_VERSION-$PROD_NODE_DISTRO.tar.xz -C /usr/local/node

step 2. Set the environment variable /etc/profile or ~/.profile, add below to the end

# Nodejs
PROD_NODE_VERSION=v10.15.0
PROD_NODE_DISTRO=linux-x64
export PATH=/usr/local/node/node-$PROD_NODE_VERSION-$PROD_NODE_DISTRO/bin:$PATH

step 3. Refresh profile

source ~/.profile
# or
source /etc/profile

npm

cli-documentation[2]

npmrc[3]

vim ~/.npmrc

# add
registry = "https://registry.npm.taobao.org/"
npm config ls -l

npm config set prefix "E:\Repos\npm"

npm config set cache "E:\Repos\npm\cache"

npm config set registry https://registry.npm.taobao.org

npm config get registry
# create a package.json file
npm init

# Generate a plain old package.json
mkdir my-npm-pkg && cd my-npm-pkg
git init
npm init -y

pm2

PM2 is daemon process manager that will help you manage and keep your application online.

github[4]

home[5]

doc[6]

# 安装
npm install pm2@latest -g

# @see https://pm2.keymetrics.io/docs/usage/startup/
# Detect available init system, generate configuration and enable startup system
pm2 startup

## Target path
## /etc/systemd/system/pm2-root.service

# 开机启动
systemctl enable pm2-root

# Once you started all the applications you want to manage, you have to save the list you wanna respawn at machine reboot with:
pm2 save

cat /etc/systemd/system/pm2-root.service

[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/node/bin/:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/local/node/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/node/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/node/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

cheatsheet

# Fork mode
pm2 start app.js --name my-api # Name process

# Cluster mode
pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
pm2 start app.js -i max      # Same as above, but deprecated.
pm2 scale app +3             # Scales `app` up by 3 workers
pm2 scale app 2              # Scales `app` up or down to 2 workers total

# Listing

pm2 list               # Display all processes status
pm2 jlist              # Print process list in raw JSON
pm2 prettylist         # Print process list in beautified JSON

pm2 describe 0         # Display all informations about a specific process
pm2 info 0             # Display all informations about a specific process

pm2 monit              # Monitor all processes

# Logs

pm2 logs [--raw]       # Display all processes logs in streaming
pm2 flush              # Empty all log files
pm2 reloadLogs         # Reload all logs

# Actions

pm2 stop all           # Stop all processes
pm2 restart all        # Restart all processes

pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)

pm2 stop 0             # Stop specific process id
pm2 restart 0          # Restart specific process id

pm2 delete 0           # Will remove process from pm2 list
pm2 delete all         # Will remove all processes from pm2 list

# Misc

pm2 reset <process>    # Reset meta data (restarted time...)
pm2 updatePM2          # Update in memory pm2
pm2 ping               # Ensure pm2 daemon has been launched
pm2 sendSignal SIGUSR2 my-app # Send system signal to script
pm2 start app.js --no-daemon
pm2 start app.js --no-vizion
pm2 start app.js --no-autorestart

Nginx as a HTTP proxy

Nginx as a HTTP proxy[7]

upstream my_nodejs_upstream {
    server 127.0.0.1:3000;
    keepalive 64;
}

server {
    listen 443 ssl;

    server_name www.my-website.com;
    ssl_certificate_key /etc/ssl/main.key;
    ssl_certificate     /etc/ssl/main.crt;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_pass http://my_nodejs_upstream/;
        proxy_redirect off;
        proxy_read_timeout 240s;
    }
}

library

async

Async[8] is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node.js and installable via npm install async, it can also be used directly in the browser. A ESM version is included in the main async package that should automatically be used with compatible bundlers such as Webpack and Rollup.

bluebird

Bluebird[9] is a fully featured promise library with focus on innovative features and performance


cli-table

cli-table[10] This utility allows you to render unicode-aided tables on the command line from your node.js scripts.

colors

colors[11] get color and style in your node.js console

commander

commander[12] The complete solution for node.js command-line interfaces, inspired by Ruby's commander

consola

consola[13] Elegant Console Logger for Node.js and Browser

Chalk

Chalk[14] Terminal string styling done right


ky

Ky[15] is a tiny and elegant HTTP client based on the browser Fetch API

got

got[16] Human-friendly and powerful HTTP request library for Node.js

axios

axios[17] Promise based HTTP client for the browser and node.js

request

Request[18] is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

request-promise

The simplified HTTP request client 'request' with Promise support. Powered by Bluebird.


pm2 process manager

PM2[19] is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

jake

jake[20] the JavaScript build tool for Node.js

puppeteer

Puppeteer[21] is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.

playwright

playwright[22] is a Node.js library to automate Chromium, Firefox and WebKit with a single API.

generic-pool

generic-pool[23] Generic resource pool with Promise based API. Can be used to reuse or throttle usage of expensive resources such as database connections.

yo

Yeoman[24] helps you to kickstart new projects, prescribing best practices and tools to help you stay productive.

rxjs

RxJS[25]: Reactive Extensions For JavaScript

lodash

lodash[26] A modern JavaScript utility library delivering modularity, performance, & extras.

ramda

ramda[27] A practical functional library for JavaScript programmers.

immer

immer[28] Create the next immutable state tree by simply modifying the current tree

date-fns

date-fns[29] provides the most comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.

moment

moment[30] A lightweight JavaScript date library for parsing, validating, manipulating, and formatting dates.

dayjs

Day.js[31] 2kB immutable date-time library alternative to Moment.js with the same modern API

mathjs

Math.js[32] is an extensive math library for JavaScript and Node.js

prettier

Prettier[33] is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.

koa-generator

koa-generator[34] Koa application generator.

art-template

art-template[35] is a simple and superfast templating engine that optimizes template rendering speed by scope pre-declared technique, hence achieving runtime performance which is close to the limits of JavaScript. At the same time, it supports both NodeJS and browser.

nanoid

nanoid[36] A tiny (108 bytes), secure, URL-friendly, unique string ID generator for JavaScript

node-fetch

node-fetch[37] A light-weight module that brings window.fetch to Node.js

zx

zx[38] A tool for writing better scripts

blessed

blessed[39] A high-level terminal interface library for node.js.

blessed-contrib

blessed-contrib[40] Build terminal dashboards using ascii/ansi art and javascript

docsify

docsify[41] A magical documentation site generator.

node-mssql

node-mssql[42]

Promises, Node, Tedious, Azure SQL. Oh My![43]

cheerio

cheerio[44] The fast, flexible, and elegant library for parsing and manipulating HTML and XML. jQuery

Framework

  • express[45]
  • meteor[46]
  • nestjs[47] A progressive Node.js framework for building efficient, reliable and scalable server-side applications. dynamic-databases-connections-with-nestjs[48] nestjs-dynamic-databases[49]
  • koa[50]
  • sails[51]
  • fastify[52]
  • egg[53]
  • loopback[54]
  • hapi[55]
  • pomelo[56]
  • node-restify[57]
  • egg[58]
  • thinkjs[59]

Package Tool

  • pkg[60] Package your Node.js project into an executable

Presets

  • neutrino[61] Create and build modern JavaScript projects with zero initial configuration. Neutrino combines the power of webpack with the simplicity of presets.

Build Tool

  • webpack[62]
  • vite[63]
  • parcel[64] The zero configuration build tool for the web.
  • Bun[65]
  • esbuild[66]
  • Gulp[67]
  • SWC[68]
  • Turbopack[69]
  • Rollup[70]
  • Nx[71]
  • Rspack[72]
  • Rolldown[73]
  • Farm[74]
  • Rsbuild[75]

Node.js On Windows XP

  • which-nodejs-version-should-i-set-up-on-an-old-windows-xp-machine[76]
  • v5.12.0[77]

引用链接

  • [1] How to install Node.js via binary archive on Linux: https://github.com/nodejs/help/wiki/Installation#how-to-install-nodejs-via-binary-archive-on-linux
  • [2] cli-documentation: https://docs.npmjs.com/cli-documentation/cli
  • [3] npmrc: https://docs.npmjs.com/files/npmrc
  • [4] github: https://github.com/Unitech/pm2
  • [5] home: https://pm2.keymetrics.io
  • [6] doc: https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/
  • [7] Nginx as a HTTP proxy: https://pm2.keymetrics.io/docs/tutorials/pm2-nginx-production-setup
  • [8] Async: https://www.npmjs.com/package/async
  • [9] Bluebird: https://www.npmjs.com/package/bluebird
  • [10] cli-table: https://www.npmjs.com/package/cli-table
  • [11] colors: https://www.npmjs.com/package/colors
  • [12] commander: https://www.npmjs.com/package/commander
  • [13] consola: https://www.npmjs.com/package/consola
  • [14] Chalk: https://www.npmjs.com/package/chalk
  • [15] Ky: https://www.npmjs.com/package/ky
  • [16] got: https://www.npmjs.com/package/got
  • [17] axios: https://www.npmjs.com/package/axios
  • [18] Request: https://www.npmjs.com/package/request
  • [19] PM2: https://www.npmjs.com/package/pm2
  • [20] jake: https://www.npmjs.com/package/jake
  • [21] Puppeteer: https://www.npmjs.com/package/puppeteer
  • [22] playwright: https://www.npmjs.com/package/playwright
  • [23] generic-pool: https://www.npmjs.com/package/generic-pool
  • [24] Yeoman: https://www.npmjs.com/package/yo
  • [25] RxJS: https://www.npmjs.com/package/rxjs
  • [26] lodash: https://www.npmjs.com/package/lodash
  • [27] ramda: https://www.npmjs.com/package/ramda
  • [28] immer: https://www.npmjs.com/package/immer
  • [29] date-fns: https://www.npmjs.com/package/date-fns
  • [30] moment: https://www.npmjs.com/package/moment
  • [31] Day.js: https://github.com/iamkun/dayjs/
  • [32] Math.js: https://www.npmjs.com/package/mathjs
  • [33] Prettier: https://www.npmjs.com/package/prettier
  • [34] koa-generator: https://www.npmjs.com/package/koa-generator
  • [35] art-template: https://www.npmjs.com/package/art-template
  • [36] nanoid: https://github.com/ai/nanoid/
  • [37] node-fetch: https://www.npmjs.com/package/node-fetch
  • [38] zx: https://github.com/google/zx
  • [39] blessed: https://github.com/chjj/blessed
  • [40] blessed-contrib: https://github.com/yaronn/blessed-contrib
  • [41] docsify: https://github.com/docsifyjs/docsify/
  • [42] node-mssql: https://github.com/tediousjs/node-mssql
  • [43] Promises, Node, Tedious, Azure SQL. Oh My!: https://devblogs.microsoft.com/azure-sql/promises-node-tedious-azure-sql-oh-my/
  • [44] cheerio: https://github.com/cheeriojs/cheerio
  • [45] express: https://github.com/expressjs/express
  • [46] meteor: https://github.com/meteor/meteor
  • [47] nestjs: https://github.com/nestjs/nest
  • [48] dynamic-databases-connections-with-nestjs: https://jnesis.com/en/blog/dynamic-databases-connections-with-nestjs/
  • [49] nestjs-dynamic-databases: https://github.com/jnesiscompany/nestjs-dynamic-databases
  • [50] koa: https://github.com/koajs/koa
  • [51] sails: https://github.com/balderdashy/sails
  • [52] fastify: https://github.com/fastify/fastify
  • [53] egg: https://github.com/eggjs/egg
  • [54] loopback: https://github.com/strongloop/loopback
  • [55] hapi: https://github.com/hapijs/hapi
  • [56] pomelo: https://github.com/NetEase/pomelo
  • [57] node-restify: https://github.com/restify/node-restify
  • [58] egg: https://github.com/eggjs/egg
  • [59] thinkjs: https://github.com/thinkjs/thinkjs
  • [60] pkg: https://github.com/vercel/pkg
  • [61] neutrino: https://github.com/neutrinojs/neutrino/
  • [62] webpack: https://github.com/webpack/webpack
  • [63] vite: https://github.com/vitejs/vite
  • [64] parcel: https://github.com/parcel-bundler/parcel
  • [65] Bun: https://github.com/oven-sh/bun
  • [66] esbuild: https://github.com/evanw/esbuild
  • [67] Gulp: https://github.com/gulpjs/gulp
  • [68] SWC: https://github.com/swc-project/swc
  • [69] Turbopack: https://github.com/vercel/turbo
  • [70] Rollup: https://github.com/rollup/rollup
  • [71] Nx: https://github.com/nrwl/nx
  • [72] Rspack: https://github.com/web-infra-dev/rspack
  • [73] Rolldown: https://github.com/rolldown/rolldown
  • [74] Farm: https://github.com/farm-fe/farm
  • [75] Rsbuild: https://github.com/web-infra-dev/rsbuild
  • [76] which-nodejs-version-should-i-set-up-on-an-old-windows-xp-machine: https://stackoverflow.com/questions/37744391/which-nodejs-version-should-i-set-up-on-an-old-windows-xp-machine
  • [77] v5.12.0: https://nodejs.org/dist/v5.12.0/
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言