curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
curl -sL https://deb.nodesource.com/setup | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential
For Node.js v4x
curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
For Node.js v6x
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
For Node.js 0.12x
curl --silent --location https://rpm.nodesource.com/setup | bash -
yum -y install nodejs
yum groupinstall 'Development Tools'
sudo apt-get update
sudo apt-get install build-essential libssl-dev
sudo yum update
sudo yum groupinstall 'Development Tools'
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
command -v nvm
nvm ls-remote
...
v5.9.0
v5.9.1
v5.10.0
v5.10.1
v5.11.0
v5.11.1
v6.0.0
v6.1.0
v6.2.0
v6.2.1
nvm install version
nvm install v6.2.1
Downloading https://nodejs.org/dist/v6.2.1/node-v6.2.1-linux-x64.tar.xz...
######################################################################## 100.0%
Now using node v6.2.1 (npm v3.9.3)
Creating default alias: default -> v6.2.1
nvm use v5.11.1
nvm alias default v5.11.1
nvm ls
v0.11.13
-> v5.11.1
v6.2.1
default -> v5.11.1
node -> stable (-> v6.2.1) (default)
stable -> 6.2 (-> v6.2.1) (default)
unstable -> 0.11 (-> v0.11.13) (default)
iojs -> N/A (default)
In this output you can see a list of all installed versions. -> indicates the version which you are currently using. default -> tag indicates the default version of Node.js in your machine.
要创建新文件,请运行以下代码:
nano myserver.js
现在将以下代码添加到文件中:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Node.js is running a server\nHi There');
}).listen(8080);
console.log('HTTP server running on port 8080.');
现在保存文件并退出编辑器。通过执行以下命令运行代码:
node myserver.js
您将在终端上看到以下输出:
HTTP server running on port 8080.
您现在可以转到浏览器并访问您的http服务器
http://your_ip_addr:8080
您将在页面上看到以下消息:
在任何Linux机器上安装Node.js有几种不同的方法,但建议使用nvm,因为它提供了更多的灵活性,您可以在任何操作系统上使用安装程序脚本。