[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.js
by 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.js
by 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.js
by 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 install
command.
npm install underscore
4.6 Installing Global Modules
Now install the coffee-script
module, but install it globally so you can use the coffee
executable 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 ...
随机推荐
- maven spring整合mybatis是使用junit测试报字节序列的错误
转载 http://blog.csdn.net/hjun01/article/details/40858753 错误信息 com.sun.org.apache.xerces.internal.impl ...
- shell top解析
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按键来不 ...
- C#封装StackExchange.Redis操作
using System; using StackExchange.Redis; using System.Collections.Generic; using System.Linq; using ...
- Failed to resolve directive: el vue2报错
vue2报错 Failed to resolve directive: el 为什么会报这个错呢,主要还是因为vue升级的时候,v-el在vue2.x以后被淘汰.使用新的标签ref替换v-el,接下来 ...
- Python操作Mongo数据库
连接数据库 import pymongo # 连接到数据库,以下两种方式均可 client = pymongo.MongoClient(host='localhost', port=27017) cl ...
- python - 在Windows系统中安装Pygame及导入Eclipse
环境:python3.6(只有一个版本)+ windows10(64 bit) + Eclipse+pydev python3.6安装完成后,会自带 easy_install 和 pip3,在Win ...
- system.ComponentModel.Win32Exception (0x80004005): 目录名无效。 解决方法
有时候我们需要在程序中调用 cmd.exe 执行一些命令 比如 我们会在程序写到 /// <summary> /// 执行Cmd命令 /// </summary> /// & ...
- 深入浅出JDBC-快速入门
一.目录 二.概述 简述 JDBC是什么?JDBC英文名为:Java Data Base Connectivity(Java数据库连接),官方解释它是Java编程语言和广泛的数据库之间独立于数据库的连 ...
- iOS开发之int,NSInteger,NSUInteger,NSNumber的使用
1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...
- Java程序生成linechart report的方法
iReport一般是一个设计阶段的工具.用来设计出报表的排版和内容.报表的动态生成须要程序来实现(毕竟报表的数据是动态的,数量是非常多的,不可能用iReport Preview的方式一个个手工去生成) ...