文章出處
文章列表
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; }); };
文章列表
全站熱搜