[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" } }
相关文章
- Module Error (from ./node_modules/eslint-loader/index.js):解决办法
- highcharts.js的时间轴折线图
- Node.js压缩与解压数据
- [Node.js] Handle Relative Paths in a Node.js ESM Project with import.meta.url
- [Node.js] Load balancing a Http server
- [Node.js] Exporting Modules in Node
- [Node.js] Scraping Dynamic JavaScript Websites with Nightmare
- [Whole Web, Node.js, PM2] Configuring PM2 for Node applications
- [Node.js] Level 5. Express
- [Node.js] What 'npm install' give us?
- js之string操作符
- [Node.js] Setup Local Configuration with Node.js Applications
- [Node.js] Identify memory leaks with nodejs-dashboard
- 【网址收藏】如何设置node.js存储路径:node缓存、数据目录
- [FAQ] JS 时间戳格式化为 date
- Atitit vue.js 把ajax数据 绑定到form表单
- 关于 Node.js 参数 max-old-space-size
- 使用 Node.js Express 的最佳实践
- 14 【node js 6/7】
- js 提取某()特殊字符串长度
- JS:正则RegExp对象匹配中文汉字
- Node.js学习笔记(一)——Node.js概要、NPM与package.json
- Node.js 博客实例(五)编辑与删除功能
- three.js 源代码凝视(十)Math/Line3.js
- js ajax 1
- 【Deepin 20系统】Jupyter notebook解决ValueError: Please install Node.js and npm before continuing installa
- Node.js stream模块(二)可写流
- js随机飘动的广告图片代码demo效果示例(整理)
- Node.js学习笔记——包管理工具