安装nodejs
sudo apt-get install nodejs #安装node
sudo apt install nodejs-legacy #安装
sudo apt install npm #安装npm
全局安装n管理器(用于管理nodejs版本)
sudo npm install n -g
安装nodejs各类版本(stable版本)
sudo n stable #稳定版本
sudo n latest #最新版本
sudo n lts #最新LTS版本
查看安装的nodejs
sudo node -v
一个官方demo
一旦你已经安装了 Node,让我们尝试构建第一个 Web 服务器。 请创建一个“app.js”文件,黏贴以下代码:
consthttp=require('http');
consthostname='127.0.0.1';
constport=3000;
constserver=http.createServer((req,res)=>{res.statusCode=200;res.setHeader('Content-Type','text/plain');res.end('Hello World\n');});
server.listen(port,hostname,()=>{console.log(`Server running at http://${hostname}:${port}/`);});
然后使用 node app.js 运行程序,访问 http://localhost:3000,你就会看到一个消息,写着“Hello World”。