zoukankan      html  css  js  c++  java
  • Nodejs

    1 javascript事件模式

    2 javascript面向对象

    3 包机制

    4 工具类库

    • npm:NodeJs包管理器
    • express:服务器端比较流行的MVC框架,处理服务请求,路由转发,逻辑处理
    • mongoose:mongodb包装,更方便使用数据库
    • :实现服务端和客户端socket通信解决方案
    • backbone:客户端MVC框架,编写客户端应用(豆瓣说)
    • coffeescript:提高JavaScript的可读性,健壮性
    • zombie:浏览器子集,编写html解析器,轻形javascript客户端测试

    -----
    模块

    require 加载js json

     
    var foo1 = require('./foo');
    var foo2 = require('./foo.js');
    var foo3 = require('/home/user/foo');
    var foo4 = require('/home/user/foo.js');
    var data = require('./data.json');

    exports 导出对象

    exports.hello = function () {
        console.log('Hello World!');
    };

    module   访问到当前模块的一些相关信息  替换当前模块的导出对象

    module.exports = function () {
        console.log('Hello World!');
    };

    模块路径解析规则

    内置模块

    如果传递给 require 函数的是 NodeJS 内置模块名称,不做路径解析,直接返回内部模块的导出对象,例如 require('fs')。

    node_modules 目录

     例如某个模块的绝对路径是 /home/user/hello.js,在该模块中使用 require('foo/bar') 方式加载模块时,则 NodeJS 依次尝试使用以下路径。

    /home/user/node_modules/foo/bar
    /home/node_modules/foo/bar
    /node_modules/foo/bar

    从当前目录递归的向上找“node_modules”

    NODE_PATH 

    NodeJS 允许通过 NODE_PATH 环境变量来指定额外的模块搜索路径。

    NODE_PATH 环境变量中包含一到多个目录路径,路径之间在 Linux 下使用:分隔,在 Windows 下使用;分隔。例如定义了以下 NODE_PATH 环境变量:

     NODE_PATH=/home/user/lib:/home/lib

    当使用 require('foo/bar')的方式加载模块时,则 NodeJS 依次尝试以下路径。

     /home/user/lib/foo/bar
     /home/lib/foo/bar
    

      

     我们已经知道了 JS 模块的基本单位是单个 JS 文件,但复杂些的模块往往由多个子模块组成。为了便于管理和使用,我们可以把由多个子模块组成的大模块称做包,并把所有子模块放在同一个目录里。

    在组成一个包的所有子模块中,需要有一个入口模块,入口模块的导出对象被作为包的导出对象。例如有以下目录结构。

    - /home/user/lib/
        - cat/
            head.js
            body.js
            main.js

    其中 cat 目录定义了一个包,其中包含了 3 个子模块。main.js 作为入口模块,其内容如下:

    var head = require('./head');
    var body = require('./body');
    
    exports.create = function (name) {
        return {
            name: name,
            head: head.create(),
            body: body.create()
        };
    };

    index.js

     以下两条语句等价。

    var cat = require('/home/user/lib/cat');
    var cat = require('/home/user/lib/cat/index');

    package.json

    - /home/user/lib/
        - cat/
            + doc/
            - lib/
                head.js
                body.js
                main.js
            + tests/
            package.json
    其中package.json内容如下。

    {
        "name": "cat",
        "main": "./lib/main.js"
    }

    如此一来,就同样可以使用 require('/home/user/lib/cat')的方式加载模块。NodeJS 会根据包目录下的 package.json 找到入口模块所在位置。

     

    命令行程序






  • 相关阅读:
    Windows Azure Cloud Service (14) 使用Windows Azure诊断收集日志记录数据
    Windows Azure Cloud Service (13) 用Visual Studio 2010 将应用程序部署到Windows Azure平台
    Windows Azure Cloud Service (15) 多个VM Instance场景下如何处理ASP.NET Session
    Windows Azure Storage (5) Windows Azure Drive
    Windows Azure Storage (7) 使用工具管理Windows Azure Storage
    SQL Azure(二) SQL Azure vs SQL Server
    webbrowser的自动提交
    提取视频的背景声音的软件
    Listview列排序的bug原因
    两个奇怪的问题
  • 原文地址:https://www.cnblogs.com/mingjing/p/7985876.html
Copyright © 2011-2022 走看看