Mac下nodeJS初体验

Mac下nodeJS初体验

这两天博主出门在外,抽空体验一下大名鼎鼎的node

安装

brew install node

安装测试

$ node -v
v8.4.0

运行本地脚本

用文本编辑器编辑一段js脚本,测试提供的Javascript环境

helloworld.js

$ node helloworld.js
Hello World
3

用node命令也可以进入交互式shell模式。

helloworld

helloworld.js

// 载入http模块
var http = require("http")
// 创建服务器
http.createServer(function(request, response){
    //发送HTTP头部
    //HTTP状态:200:OK
    //内容类型:text/plain
    response.writeHead(200,{'Content-Type': 'text/plain'});
    //发送响应数据
    response.end("Hello World!");
}).listen(8000); //服务器在8000端口监听
//终端打印信息
console.log("Server running at http://127.0.0.1:8000/");

用node运行脚本后,在本地浏览器访问http://127.0.0.1:8000/就可以看到helloworld了。

初体验先到此为止,开发工具的话推荐VScode

原文地址:https://www.cnblogs.com/fanghao/p/7808848.html