macOS下搭建Node.js环境

来源:互联网 发布:中搜网络到底怎么了 编辑:程序博客网 时间:2024/05/21 17:48

1.安装Homebrew

$:ruby -e "$(curl -fsSl http://raw.githubusercontent.com/Homebred/install/master/install);"

2.安装Node

$:brew install node

需要稍微等待一会,看到如下界面即安装成功

这里写图片描述

3.查看Node版本:

$:node -v

测试:

在桌面新建一个test.js的文件

$:cd Desktop/$:touch test.js$:vim test.js

写入以下代码

var http = require('http')var data = {name:'stevin',location:'Beijing'};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');});

编写完后保存并且退出:wq编辑状态, 然后执行$:node Desktop/test.js运行,显示:

listening on localhost:8080

第一次系统会询问是否同意接入网络, 点击同意即可,然后在浏览器地址栏键入:localhost:8080, 即可显示数据

{"name":"stevin","location":"Beijing"}

ctrl+c结束运行

0 0
原创粉丝点击