4.2 Missing Exports

Notice the two different files: high_five.js on the left side andapp.js on the right. The code as it's written will not work,high_five.js isn't exporting anything.

Add the proper exports line to have a successful high five!

//high_five.js

var highfive = function() {
console.log("smack!!");
};
module.exports = highfive; //app.js var highfive = require('./high_five.js');
highfive();

Export A Function

Notice the app.js file with the myRequest function below. Let's refactor myRequest out to its own my_request.js module.

Move the myRequest function and the http require into my_request.js

var http = require('http');

var myRequest = function(message) {
var request = http.request('http://codeschool.com', function(response) {
response.pipe(process.stdout, { end: false });
}); request.write(message);
request.end();
}; myRequest('Hello, this is dog.');

Answer:

// app.js

myRequest('Hello, this is dog.');

//my_request.js

var http = require('http');

var myRequest = function(message) {
var request = http.request('http://codeschool.com', function(response) {
response.pipe(process.stdout, { end: false });
}); request.write(message);
request.end();
}; myRequest('Hello, this is dog.');

Export the myRequest function.

module.exports = myRequest;

Require the my_request.js module in app.js.

var myRequest = require('./my_request');
myRequest('Hello, this is dog.');

Exporting An Object

The app.js code on the right side does not work. Once again we forgot to export our functions.

In the logger.js file, export the info function so we can use it in app.jsby assigning it to the exports object.

app.js

var logger = require('./logger');

logger.info('This is some information');
logger.warn('something bad is happening');

logger.js

var warn = function(message) {
console.log("Warning: " + message);
}; var info = function(message) {
console.log("Info: " + message);
}; var error = function(message) {
console.log("Error: " + message);
};

Answer:

exports.info = function(message) {
console.log("Info: " + message);
};

In the logger.js file, export the warn function so we can use it in app.jsby assigning it to the exports object.

exports.warn = function(message) {
console.log("Warning: " + message);
};

In the logger.js file, export the error function so we can use it in app.jsby assigning it to the exports object.

exports.error = function(message) {
console.log("Error: " + message);
};

4.5 Installing Local Modules

Practice using npm by installing the npm module underscore using the npm installcommand.

npm install underscore

4.6 Installing Global Modules

Now install the coffee-script module, but install it globally so you can use the coffeeexecutable that comes with coffee-script.

npm install coffee-script -g

4.7 Dependency

Add two dependencies to our package.json file, connect andunderscore. We'll want to use version 2.1.1 of connect and version1.3.3 of underscore.

Add the connect dependency to package.json

Add the underscore dependency to package.json

{
"name": "My Awesome Node App",
"version": "1",
"dependencies": {
"connect": "2.1.1",
"underscore": "1.3.3"
}
}

4.8 Semantic Versioning

We want to make sure we are always up-to-date with the most recent patch-level changes to our dependencies when we run npm install.

Update the connect version on package.json to fetch the latest patch-levelchanges. All we have to do is add one character to the beginning of the version number.

{
"name": "My Awesome Node App",
"version": "1",
"dependencies": {
"connect": "~2.2.1",
"underscore": "1.3.3"
}
}

Now update the underscore version on package.json to fetch the latestpatch-level changes. Again, all we have to do is add one character to the beginning of the version number.

{
"name": "My Awesome Node App",
"version": "1",
"dependencies": {
"connect": "~2.2.1",
"underscore": "~1.3.3"
}
}

[Node.js] 4. Modules的更多相关文章

  1. node --experimental-modules & node.js ES Modules

    node --experimental-modules & node.js ES Modules how to run esm modules in node.js cli $ node -v ...

  2. Node.js & ES Modules & Jest

    Node.js & ES Modules & Jest CJS & ESM CommonJS https://en.wikipedia.org/wiki/CommonJS ht ...

  3. Node.js & ES modules & .mjs

    Node.js & ES modules & .mjs Node.js v13.9.0 https://nodejs.org/api/esm.html https://nodejs.o ...

  4. [Node.js] 05 - Modules and Function

    一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...

  5. [Node.js] Exporting Modules in Node

    In this lesson, you will learn the difference between the exports statement and module.exports. Two ...

  6. [Node.js] CommonJS Modules

    CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at ...

  7. Node.js学习 - Modules

    创建模块 当前目录:hello.js, main.js // hello.js exports.world = function() { // exports 对象把 world 作为模块的访问接口 ...

  8. 【nodejs笔记1】配置webstorm + node.js +express + mongodb开发博客的环境

    1. 安装webstorm 并破解 2. 安装node (以及express框架) 至官网下载并安装.(http://nodejs.org)v0.10.32   msi  安装后测试,打开命令行, c ...

  9. Practical Node.js摘录(2018版)第1,2章。

    大神的node书,免费 视频:https://node.university/courses/short-lectures/lectures/3949510 另一本书:全栈JavaScript,学习b ...

随机推荐

  1. js基本数据类型 BigInt 和 Number 的区别

    今天在做LeetCode的一到 “加一” 的题,题目如下 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除 ...

  2. springMVC

    SpringMVC架构流程: 1.用户发送请求至前端控制器DispatcherServlet2.DispatcherServlet收到请求调用HandlerMapping处理器映射器.3.处理器映射器 ...

  3. Am335x U-boot LCD简易驱动

    参考此文档说明,自行添加相关代码: https://pan.baidu.com/s/1i5gLE89 相关代码: https://pan.baidu.com/s/1qXL8Bne 在文档说明第四步1中 ...

  4. ARP监测工具Arpwatch

    ARP监测工具Arpwatch   ARP协议是网络的基础协议.基于ARP协议的ARP攻击是局域网最为常见和有效的攻击方式.ARP攻击可以通过发送伪造的ARP包实施欺骗,实现各种中间人攻击.Arpwa ...

  5. hdu 1372Knight Moves

    E - Knight Moves Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  6. JMS 基本可靠性机制 和 事务机制

    4.3.1 基本可靠性机制4.3.1.1 控制消息的签收(Acknowledgment) 客户端成功接收一条消息的标志是这条消息被签收.成功接收一条消息一般包括如 下三个阶段:1.客户端接收消息:2. ...

  7. 「BZOJ4763」雪辉

    「BZOJ4763」天野雪辉 题目大意:有一棵 \(n\) 个点的树,树上每一个点有权值 \(a_i \leq 30000\) ,每次询问给出若干路径,求出这些路径的并上面的不同颜色数与 \(mex\ ...

  8. 20162304 实验一《Java开发环境的熟悉》实验报告

    Linux基础与Java开发环境 实验内容 1.熟悉Linux基础操作: 2.使用JDK编译.运行简单的Java程序: 实验要求 1.学习<Linux基础入门(新版)> 2.完成实验.撰写 ...

  9. hdu 3572 资源分配

    资源分配,每个时间点有m个机器可用,要将这资源分配给n个任务中的一些,要求每个任务在自己的时间范围中被分配了p[i]个资源,建图: 建立源,与每个时间点连边,容量为m,每个任务向其对应的时间段中的每个 ...

  10. Java虚拟机类加载机制--概述

    一.虚拟机类概加载概述 虚拟机将描述类的Class文件加载到内存,并对数据进行校验,转换解析和初始化,最终形成可以直接被虚拟机使用的Java类型 Java语言支持动态加载和动态连接. 二.虚拟机加载类 ...