mac 上node.js环境的安装与测试

一 摘要

如何大家之前做过web服务器的人都知道,nginx+lua与现在流行的node.js都是可以做web服务器的,前者在程序的写法和配置上要比后者麻烦,但用起来都是差不多.在这里建议大家如果对lua脚本语言不了解,可以多了解这门脚本语言,他号称是所有脚本语言执行效率是最高的一门脚本语言.底层是基于C语言的,非常好用,跨平台! 下面我就来给大家配置一下node.js环境.

二 mac node.js环境的配置

第一步:打开终端,输入以下命令安装Homebrew

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

WechatIMG2384.jpeg

第二步:安装node,在终端输入以下命令

brew install node

第三步 查看node安装成功与否

node -v

三 程序测试

第一步:新建一个文件testdemo.js
WechatIMG131.jpeg

js代码:

![Screen Shot 2017-02-10 at 10.42.31.png](http://upload-images.jianshu.io/upload_images/2216735-ad18355811a08734.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

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(8008, function(){
    console.log('listening on localhost:8080');
});
第二步:在浏览器查看:http://localhost:8008/
Screen Shot 2017-02-10 at 10.40.49.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容