nodejs创建web服务器之hello world程序


Node.js 最主要的应用是服务器序。

设计Node.js 的一个主要目的是提供高度可扩展的服务器环境。这是我们在本章开篇介绍过的Node 和V8 引擎有所区别的地方。Node 除了用V8 引擎来解析JavaScript 外,还提供了高度优化的应用库,用来提高服务器效率。比如说,HTTP模块是专为快速非阻塞式HTTP 服务器而用C 重新编写的。让我们看一下Node 采用HTTP 服务器的“Hello World”经典例子

http://localhost:8080/

第三个例子:hello world2。

node.js提供一个Buffer类用于转换不同编码的字符串。

目前支持三种类型:'ascii','utf8'与'binary'。详见这里

var Buffer = require('buffer').Buffer, 
buf = new Buffer(256), 
len = buf.write('\u00bd + \u00bc = \u00be', 0); 
console.log(len + " bytes: " + buf.toString('utf8', 0, len)); 

第四个例子:hello world3

//synopsis.js 
//synopsis 摘要, 梗概,大纲 
var http = require('http'); 
  
http.createServer(function (request, response) { 
 response.writeHead(200, {'Content-Type': 'text/plain'}); 
 response.end('Hello World\n'); 
}).listen(8124); 
  
console.log('Server running at http://127.0.0.1:8124/'); 


« 
» 

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3