美文网首页
第2章 Node.js入门

第2章 Node.js入门

作者: 不系流年系乾坤 | 来源:发表于2016-11-01 22:42 被阅读14次

    CommonJS模块

    // Invoke 'strict' JavaScript mode
    'use strict';
    
    // Define a module variable
    var message = 'Hello';
    
    // Print message to the console
    exports.sayHello = function() {
        console.log(message);
    };
    
    // Invoke 'strict' JavaScript mode
    'use strict';
    
    // Load the 'hello' module
    var hello = require('./hello');
    
    // Use the 'hello' module sayHello() method
    hello.sayHello();
    

    // Invoke 'strict' JavaScript mode
    'use strict';
    
    // Define the module method
    module.exports = function() {
        // Define functional variable
        var message = 'Hello';
    
        // Print the message variable to the console
        console.log(message);
    };
    
    // Invoke 'strict' JavaScript mode
    'use strict';
    
    // Load the 'hello' module
    var hello = require('./hello');
    
    // Call the 'hello' module as a function
    hello();
    

    在加载模块时可以省略.js拓展名,Node会先寻找同名的文件夹,如果找不到,则寻找同名的js文件。

    相关文章

      网友评论

          本文标题:第2章 Node.js入门

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