Mac下nodeJS初体验

Mac下nodeJS初体验

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

安装

brew install node

安装测试java

$ node -v
v8.4.0

运行本地脚本

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

helloworld.jsshell

$ node helloworld.js
Hello World
3

用node命令也能够进入交互式shell模式。浏览器

helloworld

helloworld.jsbash

// 载入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编辑器

相关文章
相关标签/搜索