2016年1月28日 星期四

【Node.js】LEVEL 4 - Modules

4.2 Missing Exports

Notice the two different files: high_five.js on the left side and app.js on the right. The code as it's written will not work, high_five.js isn't exporting anything.

Task:
Add the proper exports line to have a successful high five!

high_five.js ```js var highfive = function() { console.log("smack!!"); }; module.exports = highfive; ```

app.js ```js var highfive = require('./high_five.js'); highfive(); ```

4.3 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.

Task:
Move the myRequest function and the http require into my_request.js

my_request.js ```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(); }; module.exports = myRequest; ```

app.js ```js var myRequest = require('./my_request'); myRequest('Hello, this is dog.'); ```

4.4 Exporting An Object

The app.js code on the right side does not work. Once again we forgot to export our functions.

Task 1:
In the logger.js file, export the info function so we can use it in app.js by assigning it to the exports object.

Task 2:
In the logger.js file, export the warn function so we can use it in app.js by assigning it to the exports object.

Task 3:
In the logger.js file, export the error function so we can use it in app.js by assigning it to the exports object.

```js exports.info = function(message) { console.log("Info: " + message); }; exports.warn = function(message) { console.log("Warning: " + message); }; 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 and underscore. We'll want to use version 2.1.1 of connect and version 1.3.3 of underscore.

Task 1:
Add the connect dependency to package.json

Task 2:
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.

Task:
Update the connect version on package.json to fetch the latest patch-level changes. 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.1.1", "underscore": "~1.3.3" } } ```

沒有留言:

張貼留言