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. FileBuffer-ImageBuffer 模拟PE

    这节课的重点是:模拟PE加载过程,按照运行的要求给FileBuffer拉伸放到内存当中,从 FileBuffer 到 ImageBuffer 再到 运行Buffer. PE  加载  过程: 根据si ...

  2. 洛谷——P4017 最大食物链计数

    P4017 最大食物链计数 题目背景 你知道食物链吗?Delia生物考试的时候,数食物链条数的题目全都错了,因为她总是重复数了几条或漏掉了几条.于是她来就来求助你,然而你也不会啊!写一个程序来帮帮她吧 ...

  3. 选择器(UIPickerView)

    Apple提供了两种形式的选择器:日期选择器(UIDatePicker)与自定义选择器(UIPickerView). 当用户使用日期选择器选定一个时间后,调用datePickerID.date可以直接 ...

  4. 再谈mobile web retina 下 1px 边框解决方案

    本文实际上想说的是ios8下 1px解决方案. 1px的边框在devicePixelRatio = 2的retina屏下会显示成2px,在iphone 6 plug 下,更显示成3px.由其影响美感. ...

  5. [BZOJ4890][TJOI2017]城市(DP)

    题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达,但是通过一条高速公路需要收 ...

  6. [MVC4]Data Annotations Extensions:无法使用EmailAddress等验证特性的解决方法

    本文地址:http://www.cnblogs.com/egger/p/3404159.html  欢迎转载 ,请保留此链接๑•́ ₃•̀๑! 数据注解(Data Annotations) Web应用 ...

  7. SpringMvc配置详解

    在此做一个对Mvc的复习,便于以后快速复习mvc配置. 开发环境 : IDe :iDEA JDK:1.8 使用的框架技术:Mybtais ,Spring,Spring MVC 数据源选用Dbcp 首先 ...

  8. MySQL数据库单例连接简单实现(MySQL扩展)

    <?php /** * MySQL数据库单例实现 * * @author shizq at 2015-04-22 * */ final class MySQLFactory { private ...

  9. luoguoj 1598 垂直柱状图 模拟

    P1598 垂直柱状图 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.luogu.org/problem/show?pid=1598 ...

  10. ROS知识(5)----消息与服务的示例

    ROS中已经定义了较多的标准类型的消息,你可以用在这些标准类型的消息上再自定义自己的消息类型.这个在复杂数据传输很有用,例如节点和服务器进行交互时,就可能用到传输多个参数到服务器,并返回相应的结果.为 ...