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. HDU 6215 Brute Force Sorting(模拟链表 思维)

    Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  2. Kail Linux渗透测试教程之在Metasploit中扫描

    Kail Linux渗透测试教程之在Metasploit中扫描 在Metasploit中扫描 在Metasploit中,附带了大量的内置扫描器.使用这些扫描器可以搜索并获得来自一台计算机或一个完整网络 ...

  3. HDU 6134 Battlestation Operational(莫比乌斯反演)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6134 [题目大意] 求$\sum_{i=1}^{n}{\sum_{j=1}^{i}\lceil{\ ...

  4. HDU 4612 Warm up tarjan 树的直径

    Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...

  5. C++继承引入的隐藏与重写

    在区分隐藏和重写之前,先来理一理关于继承的东西... [继承] 继承是面向对象复用的重要手段,是类型之间的关系建模.通过继承一个类,共享公有的东西,实现各自本质不同的东西.简单的说,继承就是指一个对象 ...

  6. 单向可控硅(SCR)双向可控硅(TRIAC)

    双向可控硅工作原理与特点 从理论上来讲,双向可控硅可以说是有两个反向并列的单向可控硅组成,理解单向可控硅的工作原理是理解双向可控硅工作原理的基础 单向可控硅 单向可控硅也叫晶闸管,其组成结构图如图1- ...

  7. C++输出上三角/下三角/菱形/杨辉三角形

    1.输出上三角形 第一行1个星,第二行3个星,第三行5个星,第四行7个星,第五行9个星. 分析:三角形的形状由输出的空白和星组成,通过分析每一行输出几个空格,几个星,就可完成输出三角形的工作. #in ...

  8. 计蒜之道 初赛 第三场 题解 Manacher o(n)求最长公共回文串 线段树

    腾讯手机地图 腾讯手机地图的定位功能用到了用户手机的多种信号,这当中有的信号的作用范围近.有的信号作用的范围则远一些.有的信号相对于用户在不同的方位强度是不同的,有的则是在不论什么一个方向上信号强度都 ...

  9. soa文章摘抄

    from: http://blog.vsharing.com/fengjicheng/MC19136/ 浅析深究什么是SOA? (入选推荐日志,加10币)浅析深究什么是SOA? 金蝶中间件有限公司总经 ...

  10. [Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 1

    Code path: https://github.com/bluesilence/Lisp/tree/master/clojure/projects/room-escape As I have be ...