在 Debian 上学习 Node.js,可以按下面这条路线来,比较系统也不容易迷路。
sudo apt update
sudo apt upgrade -y
不推荐直接用 apt 装太老的版本,建议用 NodeSource:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
验证:
node -v
npm -v
重点掌握:
示例:
// hello.js
console.log("Hello Debian Node.js");
运行:
node hello.js
常用内置模块:
fs(文件操作)path(路径)http(HTTP 服务)events(事件)示例(简单 HTTP 服务):
const http = require('http');
http.createServer((req, res) => {
res.end('Hello World');
}).listen(3000, () => {
console.log('Server on http://localhost:3000');
});
常用命令:
npm init -y
npm install express
npm run dev
学习重点:
package.json推荐框架:Express(入门)、NestJS(工程化)
npm install express
pm2 管理进程:npm install -g pm2
pm2 start app.js
systemd 开机自启如果你愿意,我可以:
你更想往哪方面学?前端、后端、还是全栈?