node.js入门

node.js官网:http://nodejs.org/

参考资料:

           http://www.cnblogs.com/rubylouvre/archive/2010/07/15/1778403.html

           http://blog.csdn.net/kubalila/article/details/7267401

           http://baike.baidu.com/view/3974030.htm

一、下载node.js

           node.js官网首页即可下载(我下载的是windows版本    node-v0.6.17.msi   )

二、安装node.js

          狂点下一步即可(安装完成之后会自动添加到path)

三、首个node.js程序(hello World)

      1.在D盘下创建hello.js文件,内容如下

var sys = require("util");//旧版本var sys = require("sys");
sys.puts("Hello world");

       2.执行

          

四、用node.js创建一个服务端程序,用浏览器访问

      1.在D盘下创建http.js,内容如下

var sys = require("util"),
http = require("http");
http.createServer(function(request, response) {
	//response.sendHeader(200,{"Content-Type": "text/html"});//旧版本
	response.writeHead(200,{"Content-Type": "text/html"});
	response.write("Hello World!");
	response.end();
	//response.close();//旧版本
}).listen(8080);
sys.puts("Server running at http://localhost:8080/");
 

      2.运行服务端

      3.客户端访问

五、Buffer类用于转换不同编码的字符串

    1.在D盘下创建buffer.js内容如下:

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));

 2.执行

   

六、响应结束时输出内容

1.在D盘下创建文件synopsis.js内容如下:

var http = require('http');
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World
');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');

 2.执行

3.访问

原文地址:https://www.cnblogs.com/pengyan5945/p/5218345.html