第一步:在mac本上默认安装node就行
第二部:编写代码
- 使用 required 模块引入 Node.js模块
- 创建服务器:服务器用来监听客户端的请求,类似于Apache、Nginx等HTTP服务器
- 接收请求与相应请求:服务器很容易创建,客户端可以使用浏览器或终端发送 HTTP 请求,服务器接收请求后返回响应数据
// 引入第三方模块
var http = require('http')
// 创建服务器
http.createServer(function (request,response){
// 发送 HTTP 头部
// HTTP 状态值:200:OK
// 内容类型:text/plain
response.writeHead(200,{'Content-Type':'text/plain'});
// 发送相应数据 "Hello World"
response.end('Hello World');
}).listen(8888);
// 终端打印如下信息
console.log('Server running at http://localhost:8888');