• NodeJs


    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; }); };
  • 相关阅读:
    JAVA后端方面,如何快速达到能实习的程度
    如何高效地把Spring boot学到能干活的程度
    零高并发项目经验的人如何通过面试得到实践机会?
    Java学到什么程度可以面试工作?
    Java培训班学员如何找工作?如何过试用期?
    作为Java技术面试官,我如何深挖候选人的技能
    今年我拿到了期望中的收入,同时更希望能在睡后收入上有进一步的发展——2021年我的总结与思考
    程序员月薪一万到底难不难?
    自学java,如何快速地找到工作
    搞IT的应届生如何写好简历?
  • 原文地址:https://www.cnblogs.com/diligenceday/p/3473423.html
Copyright © 2020-2023  润新知