Node & Express Starting Guide

Node/JS 简明指引

配置

Node 库中国代理

# 设置 npm 库国内镜像
npm config set registry=https://registry.npm.taobao.org
# 安装 cnpm 代替 npm
npm install -g cnpm --registry=https://registry.npm.taobao.org 

Vue

cnpm install -g @vue/cli
cnpm install -g @vue/devtools

安装

Ubuntu

# Using LTS
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
npm i -g npm # 升级 npm 到最高版本。
npm i express express-generator -g # 全局安装 Express JS 框架及命令行。
express docs --hbs                 # 在目录 docs 下生成使用 express 框架的网站,并指定 handlebars 做页面引擎。
cd docs
npm i                  # 安装 express 框架依赖库。
DEBUG=docs:* npm start # 启动 express 应用,默认 node 应用都跑在 3000 端口。

目录结构

public
routes
views

macOS

brew install node@10 node@12 node@14

If you need to have node@10 first in your PATH run:
  echo 'export PATH="/usr/local/opt/node@10/bin:$PATH"' >> ~/.zshrc

For compilers to find node@10 you may need to set:
  export LDFLAGS="-L/usr/local/opt/node@10/lib"
  export CPPFLAGS="-I/usr/local/opt/node@10/include"

If you need to have node@12 first in your PATH run:
  echo 'export PATH="/usr/local/opt/node@12/bin:$PATH"' >> ~/.zshrc

For compilers to find node@12 you may need to set:
  export LDFLAGS="-L/usr/local/opt/node@12/lib"
  export CPPFLAGS="-I/usr/local/opt/node@12/include"

If you need to have node@14 first in your PATH run:
  echo 'export PATH="/usr/local/opt/node@14/bin:$PATH"' >> ~/.zshrc

For compilers to find node@14 you may need to set:
  export LDFLAGS="-L/usr/local/opt/node@14/lib"
  export CPPFLAGS="-I/usr/local/opt/node@14/include"

Windows

NVS (Node Version Switcher) 安装后,在 PowerShell 下可添加管理不同版本 Node JS。

Set-ExecutionPolicy Bypass -Scope Process
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

choco install nvs

nvs add lts
nvs use lts
nvs link lts

常用 node 工具包

sudo npm i -g yo
sudo npm i -g generator-aspnet
Author: njun
njun's picture
Updated: 2021/08/11