ubuntu下关于node的一些基本命令

安装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”。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容