Mac配置Node.js环境

打开终端输入命令:(安装brew)

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  

然后稍作等待,开始下载一些相应的依赖文件什么的,一小会以后安装完毕,最后出来的提示如下:

...
HEAD is now at cb711a2 create: add --tap option (#389)
==> Tapping homebrew/core
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 3707, done.
remote: Compressing objects: 100% (3591/3591), done.
remote: Total 3707 (delta 13), reused 2193 (delta 6), pack-reused 0
Receiving objects: 100% (3707/3707), 2.87 MiB | 280.00 KiB/s, done.
Resolving deltas: 100% (13/13), done.
Checking connectivity... done.
Tapped 3584 formulae (3,733 files, 8.9M)
==> Installation successful!
==> Next steps
Run `brew help` to get started
Further documentation: https://git.io/brew-docs
==> Homebrew has enabled anonymous aggregate user behaviour analytics
Read the analytics documentation (and how to opt-out) here:
  https://git.io/brew-analytics

  

在执行以下brew命令:

wangleideMacBook-Air:Soft wanglei$ brew
Example usage:
brew search [TEXT|/REGEX/]
brew (info|home|options) [FORMULA...]
brew install FORMULA...
brew update
brew upgrade [FORMULA...]
brew uninstall FORMULA...
brew list [FORMULA...]
Troubleshooting:
brew config
brew doctor
brew install -vd FORMULA
Brewing:
brew create [URL [--no-fetch]]
brew edit [FORMULA...]
Further help:
man brew
brew help [COMMAND]
brew home

  安装成功。

安装node.js 输入命令:

brew install node 

  检测安装是否成功:

node -v 

  测试程序:

var http =require('http');
var data={key:'value',hello:'world'};
var srv=http.createServer(function (req,res) {
    res.writeHead(200,{'Content-Type':'application/json'});
    res.end(JSON.stringify(data));

});
srv.listen(8080,function () {
    console.log('listening on localhost:8080');
});

  ls 定位到目录下

node desktop/h5/h5Code/js/test.js

  输出监听端口

在浏览器中输入;

http://localhost:8080/

  输出打印的值

原文地址:https://www.cnblogs.com/sunliyuan/p/7287756.html