Skip to content

Node.js 入门指南

Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时。

什么是 Node.js

Node.js 让 JavaScript 脱离了浏览器,可以在服务器端运行。它的核心特点:

  • 非阻塞 I/O:基于事件驱动,高并发场景表现优异
  • npm 生态:全球最大的包管理生态系统
  • 全栈统一:前后端都用 JavaScript,降低上下文切换成本

安装 Node.js

推荐使用 nvm 管理 Node.js 版本:

bash
# 安装 LTS 版本
nvm install --lts

# 切换版本
nvm use --lts

# 查看版本
node -v
npm -v

第一个 HTTP 服务器

js
const http = require('http')

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' })
  res.end('Hello, Node.js!')
})

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000')
})

npm 常用命令

命令作用
npm init初始化项目
npm install <pkg>安装包
npm install -D <pkg>安装开发依赖
npm run <script>运行脚本
npm update更新依赖

小结

Node.js 是前端开发者进入后端世界的最佳入口。后续文章会继续深入 Express、数据库、API 设计等话题。

最后更新:

Powered by VitePress & Vue 3