- Learning Node.js Development
- Andrew Mead
- 270字
- 2021-06-30 18:56:48
A simple example of the working of the exports object
Let's take a quick look at how that works. What we'll do is to define an age property using module.exports, the object we just explored over in Terminal. Also, we know that it's an object because we can see it in the preceding screenshot (exports: {}); this means that I can add a property, age, and set it equal to my age, which is 25, as shown here:
console.log('Starting notes.js');
module.exports.age = 25;
Then I can save this file and move into app.js to take advantage of this new age property. The const variable notes will be storing all of my exports, in the present case, just age.
In fs.appendFile, after the greeting.txt file, I'll add You are followed by the age. Inside template strings, we will use $ with curly braces, notes.age, and a period at the end, as shown here:
console.log('Starting app.js');
const fs = require('fs');
const os = require('os');
const notes = require('./notes.js');
var user = os.userInfo();
fs.appendFile('greetings.txt', `Hello ${user.username}! You are ${notes.age}.`);
Now our greeting should say Hello Gary! You are 25. It's getting the 25 value from our separate file (that is, note.js), which is fantastic.
Let's take a quick moment to rerun the program over in Terminal using the up arrow key and enter keys:

Back inside the app, we can open greetings.txt, and as shown in the following screenshot, we have Hello Gary! You are 25:

Using require(), we were able to require a file that we created, and this file stored some properties that were advantageous to the rest of the project.
- Web安全防護指南:基礎篇
- Truffle Quick Start Guide
- TCP/IP入門經典(第5版)
- Force.com Development Blueprints
- 網絡創新指數研究
- 互聯網安全的40個智慧洞見:2014年中國互聯網安全大會文集
- Wireshark網絡分析就這么簡單
- Mastering JavaFX 10
- 區塊鏈輕松上手:原理、源碼、搭建與應用
- Metasploit Penetration Testing Cookbook
- Yii Application Development Cookbook(Second Edition)
- 夢工廠之材質N次方:Maya材質手冊
- 物聯網頂層設計與關鍵技術
- 人人都該都懂的互聯網思維
- INSTANT LinkedIn Customization How-to