Node.js学习心得
Node.js是单线程的。它通过事件轮询(event loop)来实现并行操作,对此,我们应该要充分利用这一点 —— 尽可能的避免阻塞操作,取而代之,多使用非阻塞操作。
学习步骤:
一:配置环境
Node.js安装包及源码下载地址为:http://www.nodejs.org/en/download/。
下载成功之后进行安装。
二:创建项目
1,引入 required 模块
var http = require("http");
2,创建服务器
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\n'); }).listen(8888); // 终端打印如下信息 console.log('Server running at http://127.0.0.1:8888/');
3,运行
使用node命令执行代码
node server.js