Nodejs¶
Install-CN¶
- Install git
# centos or alibaba
sudo yum install git -y
# Ubuntu or Debian
sudo apt update
sudo apt install git -y
- 使用Git将NVM的源码克隆到本地的~/.nvm目录下,并检查最新版本
git clone https://gitee.com/mirrors/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
- 配置nvm
sudo sh -c 'echo ". ~/.nvm/nvm.sh" >> /etc/profile'
source /etc/profile
# 换mirror
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
- 查看nodejs 版本
nvm list-remote
- Install
nvm install v23.3.0 # or nvm install v24.13.0 # list nvm ls
Test¶
-
init
cd touch example.js vim example.js -
code
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
// 设置状态码为200
res.statusCode = 200;
// 设置内容类型为纯文本
res.setHeader('Content-Type', 'text/plain');
// 添加Cache-Control头部,禁止缓存
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
// 添加Pragma头部,禁止缓存(为了兼容旧版浏览器)
res.setHeader('Pragma', 'no-cache');
// 设置Expires头部为一个过去的日期,使内容立即过期。或者使用具体的过期日期,如 'Expires: Tue, 03 Jul 2001 06:00:00 GMT'
res.setHeader('Expires', '0');
// 发送响应体
res.end('Hello World\n');
});
// 监听指定的端口和主机名
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
node ~/example.js
netstat -tpln
返回的结果列表中包含端口3000,表明项目正常运行。
Install¶
-
Ubuntu
install on ubuntu18.04
-
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - - if error:
cd /etc/apt/sources.list.d
sudo mv vscode.list vscode.list.bak
sudo apt install nodejs
dean@dpc:~$ node -v
v14.15.3
dean@dpc:~$ npm -v
6.14.9
- install yarn:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global