zoukankan      html  css  js  c++  java
  • JavaScript学习笔记[4]

    JavaScript学习笔记[4]

    Node.js基本模块

    - global
    - process,代表当前Node.js进程
    
    ```
    > process === global.process;
    true
    > process.version;
    'v5.2.0'
    > process.platform;
    'darwin'
    > process.arch;
    'x64'
    > process.cwd(); //返回当前工作目录
    '/Users/michael'
    > process.chdir('/private/tmp'); // 切换当前工作目录
    undefined
    > process.cwd();
    '/private/tmp'
    ```
    

    Node.js基本内置模块

    - fs,异步读取文件,同步文件,readFileSync(),writeFileSync(),获取文件信息fs.stat(),服务器必须使用异步操作。
    
    ```
    //读取utf-8文本文件
    'use strict';
    
    var fs = require('fs');
    
    fs.readFile('sample.txt', 'utf-8', function (err, data) {
        if (err) {
            console.log(err);
        } else {
            console.log(data);
        }
    });
    ```
    

    ```
    //读取二进制图片,返回Buffer
    'use strict';
    
    var fs = require('fs');
    
    fs.readFile('sample.png', function (err, data) {
        if (err) {
            console.log(err);
        } else {
            console.log(data);
            console.log(data.length + ' bytes');
        }
    })
    ```
    

    ```
    'use strict';
    
    var fs = require('fs');
    
    fs.stat('sample.txt', function (err, stat) {
        if (err) {
            console.log(err);
        } else {
            // 是否是文件:
            console.log('isFile: ' + stat.isFile());
            // 是否是目录:
            console.log('isDirectory: ' + stat.isDirectory());
            if (stat.isFile()) {
                // 文件大小:
                console.log('size: ' + stat.size);
                // 创建时间, Date对象:
                console.log('birth time: ' + stat.birthtime);
                // 修改时间, Date对象:
                console.log('modified time: ' + stat.mtime);
            }
        }
    });
    
    isFile: true
    isDirectory: false
    size: 181
    birth time: Fri Dec 11 2015 09:43:41 GMT+0800 (CST)
    modified time: Fri Dec 11 2015 12:09:00 GMT+0800 (CST)
    ```
    

    - Node.js基本模块stream
    - Node.js基本模块http
    
    ```
    //hello.js
    'use strict';
    
    var http = require('http');
    
    var server = http.createServer(function(request, response){
        console.log(request.method + ':' + request.url);
    
        response.writeHead(200, {'Content-Type' : 'text/html'});
    
        response.end('<h1>Hello world!</h1>');
    });
    
    server.listen(8080);
    
    console.log('Server is running at http://127.0.0.1:8080/');
    ```
    

    ```
    //file_server.js    
    'use strict';
    
    var 
        fs = require('fs');
        url = require('url');
        path = require('path');
        http = require('http');
    
    var root = path.resolve(process.argv[2] || '.');
    
    console.log('Static root dir : ' + root);
    
    var server = http.createServer(function(request, response){
        var pathname = url.parse(request,url).pathname;
    
        var filepath = path.join(root, pathname);
    
        fs.stat(filepath,function(err,stats){
            if(!err && stats.isFile()){
                console.log('200' + request.url);
    
                response.writeHead(200);
    
                fs.createReadStream(filepath).pipe(response);
            } else{
                console.log('404' + request.url);
    
                response.writeHead(400);
    
                response.end('404 Not Found');
            }
        })
    });
    
    server.listen(8080);
    
    console.log('Server is running at http://127.0.0.1:8080/');
    ```
    

    • crypto
      • MD5和SHA1
      • Hmac
      • AES
      • Diffie-Hellman
  • 相关阅读:
    RCP中如何使用代码安装、运行plugins
    家传卤水秘方备了个份
    解决Activator X for bundle Y is invalid 以及 Activator not found
    【三分法】hdu2438 Turn the corner
    【01背包】洛谷P1282多米诺骨牌
    【动态规划】洛谷P1004方格取数
    【单调栈】最长不上升子序列变式,洛谷 P2757 导弹的召唤
    【埃氏筛】洛谷P3383埃氏筛模板
    【状压DP】poj3254 Corn Fields
    【动态规划】洛谷P1006传纸条
  • 原文地址:https://www.cnblogs.com/weedboy/p/7450514.html
Copyright © 2011-2022 走看看