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

posted on 2017-08-29 22:06  炮二平五  阅读(117)  评论(0编辑  收藏  举报

导航