1; 安装,搜索 “cygwin-nodejs” ,一路next就可以了;
随便一个目录新建一个js文件,名字随便起
var sys = require("sys");
sys.puts("hello world");
cmd到js文件目录,执行node js文件名字.js;
控制开就输出"hello_world"了;
( 附知识点:charset=utf-8 表示当前文档的字符集是采用utf-8的字符,也就是我们常说英文字符集;
uft-8 是Unicode的其中一个使用方式。
UTF是 Unicode Translation Format,即把Unicode转做某种格式的意思。
gbk gb2312 主要用于中文。
big5 用于繁体中文 )
2;
var sys = require("sys"); var http = require("http"); function start(){ http.createServer(function(request,response){ response.writeHead(200,{"Content-Type":"text/html"}); response.write("hello world"); response.close(); }).listen(8080); }; exports.start = start;
start(); sys.puts('http is ok')
执行 node 文件名.js
然后访问本地的8080端口即可看到服务端的"helloWorld"
3;
var http = require("http"); var url = require("url"); //增加require("url") function start() { function onRequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"+ pathname); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started."); } start(); exports.start = start;
4;
//抓取指定url页面的NodeJS
//抓取指定url页面的NodeJS var http = require('http'); var iconv = require('iconv-lite'); var url=require('url'); var html = ""; var getURL = url.parse('http://bj.soufun.com/'); var req =http.get(getURL, function (res) { res.setEncoding('binary');//or hex res.on('data',function (data) {//加载数据,一般会执行多次 html += data; }).on('end', function () { var buf=new Buffer(html,'binary');//这一步不可省略 var str=iconv.decode(buf, 'GBK');//将GBK编码的字符转换成utf8的 console.log('string_begin'); console.log(str.length); }) }).on('error', function(err) { console.log("http get error:",err); });
5;
var http = require("http"); var fs = require("fs"); var url = require("url"); //fs.readFile(url,文件读取的类型,fn(error,返回的数据){数据操作}) function start(){ http.createServer(function(request,response){ var pathname = url.parse( request.url).pathname; var ext = pathname.match(/(.[^.]+|)$/)[0]; console.log(request.url) switch(ext){ case ".css" : case ".js": fs.readFile('./html'+request.url,'utf-8',function(err,data){ if(err)throw err; response.writeHead(200,{"Content-Type":{".css" : "text/css",".js":"application/javascript"}}['.ext']); response.write(data); response.end(); }) break; default : fs.readFile('./html/index.html','utf-8',function(err,data){ if(err)throw err; response.writeHead(200,{"Content-Type":"text/html"}); response.write(data); response.end; }); }; }).listen(8888); }; start(); console.log('server is OK') exports.start = start; /* var http = require("http"); var fs = require('fs'); var url = require('url'); exports.start = function(){ http.createServer(function(request, response) { var pathname = url.parse(request.url).pathname; var ext = pathname.match(/(.[^.]+|)$/)[0];//取得后缀名 switch(ext){ case ".css": case ".js": fs.readFile("."+request.url, 'utf-8',function (err, data) {//读取内容 if (err) throw err; response.writeHead(200, { "Content-Type": { ".css":"text/css", ".js":"application/javascript" }[ext] }); response.write(data); response.end(); }); break; default: fs.readFile('./index.html', 'utf-8',function (err, data) {//读取内容 if (err) throw err; response.writeHead(200, { "Content-Type": "text/html" }); response.write(data); response.end(); }); } }).listen(8888); console.log("server start..."); } */
6:
//这个就是直接从jyeoo抓文件ID的脚本,跟Node没有任何关系
//新建卷包 var J = {}; var url = 'http://www.jyeoo.com/math/search?c=1&t=0&q=%E6%95%B0%E5%AD%A6&rn=30274&cs=0&p='; var maxLen = 10; for(var i=0; i<maxLen; i+=1){ $.ajax({url : url+i, success : function(data){ setId( data ); }, dataType : 'text'}); };
function asynExplain(url,callback){ var xhr = new XMLHttpRequest(); xhr.open('get',url,false); xhr.onreadystatechange = function(){ if(xhr.readyState === 4){ callback(xhr.responseText) } }; xhr.send(); }; function setId(html){ var obj = $('<div>'+ html +'</div>'); obj.find('.R01 a').each(function(i,e){ var href = e.getAttribute('href'), singlePage; var id = (href.match(//([^/]+$)/g)).toString().substr(1); J[id] = id; }); };