[Node.js] 4. Modules
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的更多相关文章
- 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 ...
- Node.js & ES Modules & Jest
Node.js & ES Modules & Jest CJS & ESM CommonJS https://en.wikipedia.org/wiki/CommonJS ht ...
- Node.js & ES modules & .mjs
Node.js & ES modules & .mjs Node.js v13.9.0 https://nodejs.org/api/esm.html https://nodejs.o ...
- [Node.js] 05 - Modules and Function
一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...
- [Node.js] Exporting Modules in Node
In this lesson, you will learn the difference between the exports statement and module.exports. Two ...
- [Node.js] CommonJS Modules
CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at ...
- Node.js学习 - Modules
创建模块 当前目录:hello.js, main.js // hello.js exports.world = function() { // exports 对象把 world 作为模块的访问接口 ...
- 【nodejs笔记1】配置webstorm + node.js +express + mongodb开发博客的环境
1. 安装webstorm 并破解 2. 安装node (以及express框架) 至官网下载并安装.(http://nodejs.org)v0.10.32 msi 安装后测试,打开命令行, c ...
- Practical Node.js摘录(2018版)第1,2章。
大神的node书,免费 视频:https://node.university/courses/short-lectures/lectures/3949510 另一本书:全栈JavaScript,学习b ...
随机推荐
- 洛谷P3919 【模板】可持久化数组 [主席树]
题目传送门 可持久化数组 题目描述 如题,你需要维护这样的一个长度为 $N$ 的数组,支持如下几种操作 在某个历史版本上修改某一个位置上的值 访问某个历史版本上的某一位置的值 此外,每进行一次操作(对 ...
- python计数器Count
python计数器Count # -*- coding:utf-8 -*- """ python计数器Counter 需导入模块collections "&qu ...
- Django Restframework 实践(一)
具备以下知识: django http://www.cnblogs.com/menkeyi/p/5882464.html http://www.cnblogs.com/menkeyi/p/588245 ...
- Ubuntu系统 安装谷歌 Chrome 浏览器
在 Ubuntu 16.04 中安装谷歌 Chrome 浏览器,步骤: 1.sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P / ...
- luogu P1919 【模板】A*B Problem升级版(FFT快速傅里叶)
模板 嗯 做多项式乘法,进位 没了 #include<cmath> #include<cstdio> #include<cstring> #include<a ...
- 洛谷 P1115 最大子段和
P1115 最大子段和 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度. 第2行包含N ...
- bzoj 1101
其实这个用的是Mobius反演的第二种形式 F(d) = (n div d) * (m div d) f(d) = [ gcd(i,j)=d ] (i in [1,a], j in [1,b]) /* ...
- bzoj 2440
题意:有一个从小到大的由不包含平方约数的数组成的数列,从1开始,求第k项. “满足某种限制的数的第k个”+二分答案="前n个数有多少个数满足限制“ 求[1,n]中有多少个数没有平方约数,我们 ...
- org.apache.curator:master选举和分布式锁
1. master选举(LeaderSelector) 1)LeaderSelector构造函数 在leaderPath上建立分布式锁:mutex = new InterProcessMutex(cl ...
- bzoj 2038 小Z的袜子 莫队算法
题意 给你一个长度序列,有多组询问,每次询问(l,r)任选两个数相同的概率.n <= 50000,数小于等于n. 莫队算法裸题. 莫队算法:将序列分为根号n段,将询问排序,以L所在的块为第一关键 ...