- Learning Node.js Development
- Andrew Mead
- 458字
- 2021-06-30 18:56:47
The OS module in require()
Once we have created and appended the greetings.txt file, we'll customize this greeting.txt file. To do this, we'll explore one more built-in module. We'll be using more than just appendFile in the future. We'll be exploring other methods. For this section, the real goal is to understand require(). The require() function lets us load in the module's functionality so that we can call it.
The second module that we'll be using is OS, and we can view it in the documentation. In the OS module, we'll use the method defined at the very bottom, os.userInfo([options]):

The os.userInfo([options]) method gets called and returns various information about the currently logged-in user, such as the username, and this is what we'll pull off:

Using the username that comes from the OS, we can customize the greeting.txt file so that instead of Hello world! it can say Hello Gary!.
To get started, we have to require OS. This means that we'll go back inside Atom. Now, just below where I created my fs constant, I'll create a new constant called os, setting it equal to require(); this gets called as a function and passes one argument, the module name, os, as shown here:
console.log('Starting app.');
const fs = require('fs');
const os = require('os');
fs.appendFile('greetings.txt', 'Hello world!');
From here, we can start calling methods available on the OS module, such as os.userInfo([optional]).
Let's make a new variable called user to store the result. The variable user will get set equal to os.userInfo, and we can call userInfo without any arguments:
console.log('Starting app.');
const fs = require('fs');
const os = require('os');
var user = os.userInfo();
fs.appendFile('greetings.txt', 'Hello world!');
Now, before we do anything with the fs.appendFile line, I'll comment it out and print the contents of the user variable using console.log:
console.log('Starting app.');
const fs = require('fs');
const os = require('os');
var user = os.userInfo();
console.log(user);
// fs.appendFile('greetings.txt', 'Hello world!');
This will let us explore exactly what we get back. Over in Terminal, we can rerun our program using the up arrow key and enter key, and right here in the following code, you see that we have an object with a few properties:

We have uid, gid, username, homedir, and shell. Depending on your OS, you'll not have all of these, but you should always have the username property. This is the one we care about.
This means that back inside Atom, we can use user.username inside of appendFile. I'll remove the console.log statement and uncomment our call to fs.appendFile:
console.log('Starting app.');
const fs = require('fs');
const os = require('os');
var user = os.userInfo();
fs.appendFile('greetings.txt', 'Hello world!');
Now, where we have world in the fs.appendFile, we'll swap it with user.username. There are two ways we can do this.
- 數(shù)據(jù)通信網(wǎng)絡(luò)實踐:基礎(chǔ)知識與交換機(jī)技術(shù)
- 面向云平臺的物聯(lián)網(wǎng)多源異構(gòu)信息融合方法
- 中小型局域網(wǎng)組建、管理與維護(hù)實戰(zhàn)
- 射頻通信系統(tǒng)
- NB-IoT物聯(lián)網(wǎng)技術(shù)解析與案例詳解
- 城市治理一網(wǎng)統(tǒng)管
- 物聯(lián)網(wǎng)之霧:基于霧計算的智能硬件快速反應(yīng)與安全控制
- Microservice Patterns and Best Practices
- 物聯(lián)網(wǎng)長距離無線通信技術(shù)應(yīng)用與開發(fā)
- 網(wǎng)絡(luò)AI+:2030后的未來網(wǎng)絡(luò)
- Scala Design Patterns.
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- 組網(wǎng)技術(shù)與網(wǎng)絡(luò)管理
- Guide to NoSQL with Azure Cosmos DB
- Enterprise ApplicationDevelopment with Ext JSand Spring