美文网首页全栈开发
Node.js教程(02)|基础知识篇(下)

Node.js教程(02)|基础知识篇(下)

作者: 夏海峰 | 来源:发表于2019-07-18 08:57 被阅读11次
    Node.js 模块系统

    模块系统,便于各个文件之间的相互调用。在node.js中,模块就是文件,文件也是模块,这些模块可以是js代码,也可以是JSON,还可以是经由编译过的C/C++扩展。

    创建模块

    Node.js 提供了 exports 和 require 两个对象,其中 exports 是模块公开的接口,require 用于从外部获取一个模块的接口,即所获取模块的 exports 对象。

    如果要对外暴露属性或方法,用exports就行;要暴露对象(类似class,包含了很多属性和方法),用module.exports

    (1)exports 导出模块

    // 导出模块
    exports.world = function() {
        console.log('hello world')
    }
    exports.world2 = function() {
        console.log('hello world 2')
    }
    
    // 导入模块
    var hello = require('./hello')
    hello.world()
    hello.world2()
    

    (2)module.exports 导出模块

    // 导出模块
    module.exports = function() {
        var name;
        this.setName = function(arg) {
            name = arg;
        };
        this.getName = function() {
            console.log('hello ' + name);
        };
    }
    
    // 导入模块
    var module = require('./module');
    var m = new module()
    m.setName('lucy')
    m.getName()
    

    导入模块

    Node.js 的require方法查找并导入模块的策略和优先级如下:由于 Node.js中存在 4类模块(原生模块和3种文件模块),尽管require方法极其简单,但是内部的加载却是十分复杂的,其加载优先级也各自不同。如下图所示:

    模块加载机制和优先级
    Node.js 函数

    在JavaScript中,一个函数可以作为另一个函数的参数。我们可以先定义一个函数,然后传递,也可以在传递参数的地方直接定义函数。Node.js中函数的使用与Javascript类似,示例如下:

    function execute(someFn, value) {
        someFn(value);
    }
    
    // 测试
    function say(world) {
        console.log('hello ' + world);
    }
    execute(say, 'world');
    
    // 使用匿名函数作为参数
    execute(function(arg) {
        console.log('hello ' + arg);
    }, 'ella');
    
    Node.js 路由

    在HTTP请求的 Request对象中,我们可以提取出 URL、GET/POST请求参数,为了实现这一功能,我们还需要使用 urlquerystring 模块来帮忙。

    (1)第一步,封装路由处理函数

    function route(pathname) {
        console.log('the request url is ' + pathname)
    }
    
    exports.route = route
    

    (2)第二步,封装HTTP服务器

    var http = require('http');
    var url = require('url');
    
    function start(routeFn) {
        function onRequest(req, res) {
            // 使用 url模块解析 req.url
            var pathname = url.parse(req.url).pathname;
            // 执行路由函数
            routeFn(pathname);
            // res响应数据
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.write('hello world');
            res.end();
        }
        http.createServer(onRequest).listen(8899);
        console.log('Server running at http://172.0.0.1:8899/');
    }
    
    exports.start = start
    

    (3)第三步,创建HTTP服务器

    var server= require('./start')
    var router = require('./router')
    
    server.start(router.route)
    
    Node.js 全局对象

    在浏览器 JavaScript中,通常window是全局对象;而Node.js中的全局对象是global。所有全局变量(除了global本身以外)都是global对象的属性。

    在 Node.js我们可以直接访问到global的属性,而无须引用它们。

    • __filename表示当前正在执行的脚本的文件名,它将输出文件所在位置的绝对路径。
    • __dirname表示当前执行脚本所在的目录。
    • setTimeout(cb, ms)全局函数,在指定的毫秒(ms)数后执行指定函数(cb),它只执行一次指定函数,返回一个代表定时器的句柄值。
    • clearTimeout(timer)全局函数,用于停止一个之前通过setTimeout()创建的定时器。
    • setInterval(cb, ms)全局函数,在指定的毫秒(ms)数后执行指定函数(cb)。
    • clearInterval(timer)全局函数,用来清除setInterval()定时器。
    • console用于提供控制台的标准输出,用于向标准输出流(stdout)或标准错误流(stderr)输出字符。
    • process是一个全局变量,即 global 对象的属性。它用于描述当前Node.js的进程状态,提供了一个与操作系统交互的简单接口。通常在你写本地命令行程序的时候,少不了要和它打交道。

    process 对象

    process对象提供了一系列有用的事件、属性和方法,可自行研究Node.js官方手册中的相关文档。

    // process对象提供的 exit事件
    process.on('exit', function(code) {
        setTimeout(function() {
            console.log('该代码不会执行');
        }, 0);
    
        console.log('退出码为:' + code);
    });
    
    // process对象提供了一系列有用的属性
    console.log(process.platform)
    console.log(process.version)
    
    // process对象提供了一系列有用的方法
    console.log(process.cwd())
    console.log(process.memoryUsage())
    
    image.png
    Node.js 常用工具 —— util 模块

    util是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能过于精简的不足。

    • util.inherits(constructor, superConstructor) 用于实现对象间的原型继承。
    • util.inspect(object,[showHidden],[depth],[colors]) 是一个将任意对象转换为字符串的方法,通常用于调试和错误输出。它至少接受一个参数 object,即要转换的对象。
    • util.isArray(object) 用于检测object参数是否为数组。
    • util.isRegExp(object) 用于检测object参数是否是正则表达式。
    • util.isDate(object) 用于检测object参数是否是Date日期对象。
    • util.isError(object) 用于检测object参数是否是Error错误对象。

    常用的工具模块,还有os模块、path模块、net模块、dns模块、domain模块,可自行查看node.js官方进行学习。

    Node.js 文件系统 —— fs模块

    Node.js 提供一组类似UNIX(POSIX)标准的文件操作API,这些API集成在fs模块中。

    var fs = require('fs');
    

    同步与异步

    Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFileSync()。异步的方法函数最后一个参数为回调函数,回调函数的第一个参数包含了错误信息(error)。建议大家使用异步方法,比起同步,异步方法性能更高,速度更快,而且没有阻塞。

    var fs = require('fs')
    
    // 同步读取文件
    var data = fs.readFileSync('input.txt')
    console.log(data.toString())
    
    // 异步读取文件
    fs.readFile('input.txt', function(err, data) {
        if (err) {
            return console.log(err)
        }
        console.log(data.toString())
    })
    

    文件操作 常用API

    • fs.open(path, flags[, mode], callback) 在异步模式下打开文件.
    • fs.stat(path, callback) 通过异步模式获取文件信息.
    • fs.writeFile(file, data[, options], callback) 在异步模式下写入文件.
    • fs.read(fd, buffer, offset, length, position, callback) 在异步模式下读取文件.
    • fs.close(fd, callback) 在异步模式下关闭文件.
    • fs.ftruncate(fd, len, callback) 在异步模式下截取文件.
    • fs.unlink(path, callback) 删除文件.

    目录操作 常用API

    • fs.mkdir(path[, options], callback) 创建目录。
    • fs.readdir(path, callback) 读取目录
    • fs.rmdir(path, callback) 删除目录
    GET/POST 请求处理

    表单提交到服务器一般都使用 GET/POST 请求。下面介绍在Node.js中如何处理GET/POST请求。

    GET请求处理 - 使用url模块

    var http = require('http')
    var url = require('url')
    var util = require('util')
    
    http.createServer(function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'})
        // 使用url模块处理GET请求中请求参数
        var data = url.parse(req.url, true)
        console.log(data)
        res.write('网站名:' + data.host)
        res.write('\n')
        res.write('网站URL:' + data.path)
        res.end()
    }).listen(8899)
    
    console.log('服务器已启动')
    

    POST请求处理 - 使用querystring模块

    POST 请求的内容放在请求体中,http.ServerRequest 并没有一个属性代表请求体,原因是等待请求体传输可能是一件耗时的工作。比如上传文件,而很多时候我们可能并不需要理会请求体的内容,恶意的POST请求会大大消耗服务器的资源,所以 node.js 默认是不会解析请求体的,当你需要的时候,需要手动来做。

    var http = require('http');
    var querystring = require('querystring');
    var util = require('util');
     
    http.createServer(function(req, res){
        // 定义了一个post变量,用于暂存请求体的信息
        var post = '';     
     
        // 通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
        req.on('data', function(chunk){    
            post += chunk;
        });
     
        // 在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。
        req.on('end', function(){    
            post = querystring.parse(post);
            res.end(util.inspect(post));
        });
    }).listen(3000);
    


    Node.js 完整教程 第02篇 结束!

    相关文章

      网友评论

        本文标题:Node.js教程(02)|基础知识篇(下)

        本文链接:https://www.haomeiwen.com/subject/ytghlctx.html