博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Node.js] 4. Modules
阅读量:7112 次
发布时间:2019-06-28

本文共 3859 字,大约阅读时间需要 12 分钟。

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.jsvar highfive = function() {  console.log("smack!!");};module.exports = highfive;//app.jsvar 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.jsmyRequest('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"  }}

 

转载地址:http://tilhl.baihongyu.com/

你可能感兴趣的文章
Gradle Java Web应用程序并在Tomcat上运行
查看>>
jz2440移植QT5.6【学习笔记】【原创】
查看>>
WPF 关于圆角的制作
查看>>
前端性能优化之WebP
查看>>
android studio 各种问题 应该能帮助到你们
查看>>
福州首届.NET开源社区技术交流会圆满成功
查看>>
.Net 鉴权授权
查看>>
MySql(十四):MySql架构设计——可扩展性设计之数据切分
查看>>
Ocelot简易教程(二)之快速开始1
查看>>
[Angular] Angular ngSwitch Core Directive In Detail
查看>>
JSON Web Token(JWT)使用步骤说明
查看>>
思绪:常想一二
查看>>
WPF - Group分组对ListBox等列表样式的约束
查看>>
getpwuid和getpwnam的用法
查看>>
C语言文件操作解析(一)
查看>>
matlab练习程序(Floyd–Steinberg dithering)
查看>>
Android之Handler用法总结
查看>>
《敏捷个人》周刊 第3期 (可下载)
查看>>
XPath and TXmlDocument
查看>>
JQ集合
查看>>