Node.js 组成:javascript
一、required模块:使用require指令来载入Node.js模块;java
二、建立服务器:服务器用于监听客服端的请求,相似于apache等http服务器。node
三、接收请求与响应请求:客户端使用浏览器或终端发送http请求,服务器接收请求后返回响应数据。apache
建立 Node.js 第一个 "Hello, World!" 应用。浏览器
var http = require('http');
var http = require('http'); http.createServer(function(request,response){ //发送http头部 //http状态值:200:ok //内容类型:text/plain (plain显示代码段) response.writeHead(200,{'Content-Type':'text/plain'}); //发送响应数据“hello world” response.end('hello world'); }).listen(8888); //终端打印信息 console.log('Server running at http://127.0.0.1:8888/');
node命令执行上面的代码:服务器
node server.js Server running at http://127.0.0.1:8888/
使用浏览器访问http://127.0.0.1:8888/ 浏览器将输入‘hello world’;函数