# ===== CodeLab: tool-node-env =====
# 以下代码片段按文章出现顺序拼接, 共 7 段

# ----- 片段 1 (bash) -----
# Windows 用户先装 nvm-windows(去官网下载安装包)
nvm install 20
nvm use 20
# 验证安装:下面两条命令都要有输出
node -v
npm -v

# ----- 片段 2 (bash) -----
mkdir my-first-app
cd my-first-app
npm init -y

# ----- 片段 3 (bash) -----
# 安装一个 Web 框架(写入 dependencies)
npm install express
# 安装代码检查工具(写入 devDependencies)
npm install eslint --save-dev
# 卸载依赖
npm uninstall eslint
# 查看当前项目装了哪些包
npm ls --depth=0

# ----- 片段 4 (json) -----
{
  "name": "my-first-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "eslint": "^8.57.0"
  }
}

# ----- 片段 5 (bash) -----
npm start      # 启动项目(start 可省略 run)
npm run dev    # 开发模式:文件一保存就自动重启
npm run lint   # 跑代码检查

# ----- 片段 6 (json) -----
"scripts": {
  "start": "node server.js",
  "dev": "node --watch server.js",
  "lint": "eslint ."
}

# ----- 片段 7 (javascript) -----
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
  res.end('<h1>你好,Node!</h1><p>第一个服务器启动成功</p>');
});

server.listen(3000, () => {
  console.log('服务器已启动: http://localhost:3000');
});
