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. SpringBoot学习(六)

    1.pom 文件 <?xml version="1.0" encoding="utf-8"?> <dependencies> <d ...

  2. 【Pollard-rho算法】【DFS】poj2429 GCD & LCM Inverse

    题意:给你一两个数m和n,它们分别是某对数A,B的gcd和lcm,让你求出一对使得A+B最小的A,B. n/m的所有质因子中,一定有一部分是只在A中的,另一部分是只在B中的. 于是对n/m质因子分解后 ...

  3. ZOJ 3256 Tour in the Castle 插头DP 矩阵乘法

    题解 这题是一道非常好的插头题,与一般的按格转移的题目不同,由于m很大,要矩阵乘法,这题需要你做一个按列转移的插头DP. 按列转移多少与按格转移不同,但大体上还是基于连通性进行转移.每一列只有右插头是 ...

  4. 安卓中AsyncTask的基本使用

    安卓中AsyncTask的基本使用 使用场景介绍 在安卓开发中,我们经常需要访问互联网资源,这些访问是都需要在后台线程中去完成的,因为安卓的UI线程不允许执行耗时任务.然而,后台线程是不可以修改安卓的 ...

  5. mysql事件的开启和调用

    检测事件是否开启 show variables like 'event_scheduler'; 开启事件 set global event_scheduler = on; 创建一个存储过程 delim ...

  6. Java 常用远程调用协议比较

    一.综述本文比较了RMI,Hessian,Burlap,Httpinvoker,web service等5种通讯协议的在不同的数据结构和不同数据量时的传输性能.RMI是java语言本身提供的远程通讯协 ...

  7. CentOS 6.9/7通过yum安装指定版本的Nginx

    说明:通过yum好处其实很多,环境变量不用配置,配置文件放在大家都熟悉的地方,通过rpm -ql nginx可以知道全部文件的地方等等. Nginx(1.12.2) 一.安装和配置 1.安装 # rp ...

  8. Linux下简单分析请求有没有进到本机的工具tcpdump(网络接口的数据包的头信息)

    可以通过这个工具快速分析出一个请求到底有没有进入到本机.信息有点简单,对于前期的分析比较有帮助.而对于详细的分析可以借助iptables的raw表进行日志分析. 参考: http://man.linu ...

  9. BZOJ 1032 JSOI 2007 祖码Zuma 区间DP

    题目大意:依照祖玛的玩法(任意选颜色),给出一段区间.问最少用多少个球可以把全部颜色块都消除. 思路:把输入数据依照连续的块处理.保存成颜色和数量.然后用这个来DP.我们知道,一个单独的块须要两个同样 ...

  10. JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式

    JSON JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 它基于JavaScript(Standard ECMA-262 3rd Edition - D ...